解决Thymeleaf中LocalDateTime格式转String报错

项目场景:

当把LocalDateTime类型的值赋值给layui.date组件(该日期组件的值时String格式)初始化时发现后台报错


问题描述

报错信息如下:

Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method format(java.time.LocalDateTime,java.lang.String) cannot be found on type org.thymeleaf.expression.Dates
    at org.springframework.expression.spel.ast.MethodReference.findAccessorForMethod(MethodReference.java:225)
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:135)
    at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:55)
    at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:386)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:92)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:112)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:338)
    at org.thymeleaf.spring5.expression.SPELVariableExpressionEvaluator.evaluate(SPELVariableExpressionEvaluator.java:265)
    ... 98 common frames omitted
2022-12-29 11:54:11,229-ERROR-[DirectJDKLog.java:175]-[http-nio-8888-exec-8]-Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/fourmEdit.html]")] with root cause
org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method format(java.time.LocalDateTime,java.lang.String) cannot be found on type org.thymeleaf.expression.Dates

原因分析:

我一直使用Thymeleaf的日期转String的方法:[[${#dates.format(日期值,'yyyy-MM-dd')}]]

以前从未出现问题,但以前传的日期格式是Date,因JDK1.8之后推荐使用线程安全的LocalDateTime类型,所以新项目日期格式都是使用LocalDateTime类型


解决方案:

后台属性如果是LocalDateTime类型,传到前台格式化需要使用temporals.format :
     [[${#temporals.format(日期值,'yyyy-MM-dd')}]] 

但需要注意Thymeleaf的版本,如果版本过低是无法使用temporals,


解决办法:

1.替换新版本的Thymeleaf,我新项目使用springboot2.7.4之后依赖的Thymeleaf版本是支持的;


2. 其他低版本需要从新引入高版本依赖,
          <dependency>
             <groupId>org.thymeleaf.extras</groupId>
             <artifactId>thymeleaf-extras-java8time</artifactId>
             <version>3.0.4.RELEASE</version>
         </dependency>
 

我的实例:

        html

<div class="layui-form-item">
			<div class="layui-col-xs6">
				<label class="layui-form-label">回答日</label>
				<div class="layui-input-block">
					<input id="finishdate" name="finishdate" th:value="${qualityException.finishdate}" autocomplete="off" class="layui-input" />
				</div>
			</div>
			<div class="layui-col-xs6">
				<label class="layui-form-label">完成日</label>
				<div class="layui-input-block">
					<input id="replydate" name="replydate" th:value="${qualityException.replydate}" class="layui-input" autocomplete="off">
				</div>
			</div>
		</div>

js

layui.use(['layer', 'form', 'laydate','upload', 'element'], function() {
		var $ = layui.jquery, form = layui.form, layer = layui.layer,
		   element = layui.element, laydate = layui.laydate, upload = layui.upload;
        laydate.render({
			elem: '#replydate' //指定元素
			,min: 0 //最小日期只能选择当天
			,value: [[${#temporals.format(qualityException.replydate,'yyyy-MM-dd')}]] //初始化回答日
		});
		laydate.render({
			 elem: '#finishdate'
			,min: 0 //最小日期只能选择当天
			,value:  [[${#temporals.format(qualityException.finishdate,'yyyy-MM-dd')}]]  //初始化完成日
		});


});

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

往事不堪回首..

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值