项目场景:
当把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')}]] //初始化完成日
});
});