java - thymeleaf 日期格式,而每个

标签 java thymeleaf

我正在尝试在 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/

相关文章:

spring-boot - 使用变量在 th :include - ThymeLeaf + Spring Boot 中构建片段名称

java - URL 中的 Thymeleaf 2 表达式

java - Spring 启动 thymeleaf : Unable to pick up templates from sub-folders under resources/templates

java - Thymeleaf 模板中的迭代不起作用

java - 如何为具有动态大小的 ZooKeeper 集群初始化 CuratorFramework?

Java - 返回数组中的多个字符串

java - PhantomJS 驱动程序未单击页面中的元素,但未引发错误

java - Maven,本地目录的相对路径

java - 将字母表示为数字

thymeleaf - 访问 thymeleaf 模板中的 application.properties 值