java - Thymeleaf - :text 中的类方法

标签 java spring thymeleaf

我有域类:

public class Note
{
    private Date date;
    // other fields;

    public void setDate( Date date )
    {
        this.date = date;
    }

    public Date getDate ()
    {
        return date;
    }
}

但在 Thymeleaf 中我不想直接使用字段日期。通常我可以有一种方法,比如

public String getFormattedDate()
{
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
    return dateFormat.format(date);
}

没有名为 formattedDate 的字段(我不需要它)。

但是..我不能那样做:

th:text = "${note.formattedDate}"

我可以添加一个名为 formattedDate + getters 和 setters 的字段,但在我看来这是非常糟糕的解决方案,因为我只需要格式化日期的值。

你有办法解决这个问题吗?

最佳答案

最好的解决方案是使用

${#dates.format(date, YOUR_PATTERN, #locale)} 

关于java - Thymeleaf - :text 中的类方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37225091/

相关文章:

spring - EL1007E : Property or field **** cannot be found on null

spring - 对于 Thymeleaf 中的每个运算符

java - 如何自动调整 JFrame 上图表的大小?

java - 如何使 A runnable 多次随机运行?

java - 使用 AbstractTransactionalJUnit4SpringContextTests : changes in DB not reverted after test method 测试纯 JDBC DAO 方法

java - Spring 上下文和 Bean 生命周期回调 : practical examples of usage

html - 当用户无法使用 Spring Boot 登录时,如何在登录页面中显示错误消息?

java - Tensorflow-Java 图像比较

java - 如何在需要注入(inject)实例的 Guice 中进行动态绑定(bind)?

html - CSS 样式不适用于 Thymeleaf 的电子邮件模板