Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java th:text中的Thymeleaf类方法_Java_Spring_Thymeleaf - Fatal编程技术网

Java th:text中的Thymeleaf类方法

Java th:text中的Thymeleaf类方法,java,spring,thymeleaf,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 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的字段,但我认为这是一个非常糟糕的解决方案,因为我只需要formattedDate的值


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

最好的解决方案是使用

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

最好的解决办法是使用

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

请发布异常和错误详细信息
th:text=“${note.formattedDate}”
是有效的表达式。我猜您的错误在其他地方。错误:“在类eniupage.domain.News中找不到属性formattedDate的setter”,但为什么我的表达式有效?如果使用“${note.date}”,它可以正常工作,但格式不好。您也可以这样做:
th:text=“${note.getFormattedDate()}
th:text=“${dates.format(note.date,'yyyyy/MM/dd HH:MM:ss')”
请参阅。感谢链接;${note.getFormattedDate()}不起作用。这是hibernate的异常:org.hibernate.PropertyNotFoundException:找不到setter。错误表明有人试图将值
formattedDate
设置为属性,或者将
formattedDate
解释为属性,因为方法是
get…
。完整的堆栈跟踪可能会有所帮助。但是我很好当然这不是Thymeleaf问题。我当前使用的getter方法没有任何setter或属性。请发布异常和错误详细信息。
th:text=“${note.formattedDate}”
是一个有效的表达式。我猜您的错误在其他地方。错误:“在eniupage.domain.News类中找不到属性formattedDate的setter。”“但是为什么我的表达是正确的呢?如果使用“${note.date}”,它可以正常工作,但格式不好。您也可以这样做:
th:text=“${note.getFormattedDate()}
th:text=“${dates.format(note.date,'yyyyy/MM/dd HH:MM:ss')”
请参阅。感谢链接;${note.getFormattedDate()}不起作用。这是hibernate的异常:org.hibernate.PropertyNotFoundException:找不到setter。错误表明有人试图将值
formattedDate
设置为属性,或者将
formattedDate
解释为属性,因为方法是
get…
。完整的堆栈跟踪可能会有所帮助。但是我很好当然这不是Thymeleaf问题。我目前使用的getter方法没有任何setter或属性。