编码时发现thymeleaf解析java8的LocalDateTime类型时间时会报错。
网上的解决方案是引入thymeleaf-extras-java8time,其实如果使用spring-boot的话,在spring-boot-starter-thymeleaf已经默认引入了这个包,不需要在额外引入,但是需要加入配置 addDialect(new Java8TimeDialect());
,具体配置如下
@Configuration
@EnableWebMvc
public class ThymeleafConfig extends WebMvcAutoConfiguration {
//并不是主需要这一个配置,其他配置已省略
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
engine.addDialect(new Java8TimeDialect());
return engine;
}
}
在页面里就可以正常解析了
<td th:text="${#temporals.format(user.createTime,'yyyy-MM-dd HH:mm:ss')}"></td>