我正在尝试在 thymeleaf 中执行 forEach 时应用dates.format。但我收到了这条消息
org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating OGNL expression: "e.datesCoordinates.created" (template: "templates/alarms" - line 262, col 48)
如果我在“th:each”之外执行它,它会完美地工作。我怎样才能让它发挥作用?
<div class="content" th:each="e : ${events}">
<div class="info date" th:value="${e.datesCoordinates.created}? ${#dates.format(e.datesCoordinates.created, 'dd/MM/yyyy HH:mm')}"></div>
<div class="info operator" th:text="|${e.owner.first_name} ${e.owner.last_name}|"></div>
</div>
最佳答案
由于 e.datesCooperatives.created 是一个字符串,因此您需要先解析它,然后才能格式化它。下面的代码应该可以工作。
<th:block th:with="sdf = ${new java.text.SimpleDateFormat('dd/MM/yyyy HH:mm')}">
<div class="content" th:each="e : ${events}">
<div class="info date" th:value="${e.datesCoordinates.created}? ${#dates.format(sdf.parse(e.datesCoordinates.created), 'dd/MM/yyyy HH:mm')}"></div>
<div class="info operator" th:text="|${e.owner.first_name} ${e.owner.last_name}|"></div>
</div>
</th:block>
重要
当使用new java.text.SimpleDateFormat
时,您需要像当前字符串格式一样匹配表达式。例如,如果您要保存10-03-2018,那么您的代码将如下所示 ${new java.text.SimpleDateFormat('dd-MM-yyyy')}
.
关于java - thymeleaf 日期格式,而每个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53227536/