Thymeleaf-EL1004E:方法调用:在类型org.thymeleaf.expression.Calendars上找不到方法year(java.util.Date)

时间:2020-09-22 09:08:06

标签: thymeleaf

我具有以下配置来呈现Thymeleaf模板:

SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setTemplateMode("HTML");
resolver.setCharacterEncoding("UTF-8");
resolver.setPrefix(pathPrefix);
resolver.setSuffix(".html");
resolver.setOrder(1);
resolver.setCacheable(false);

SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setDialect(new SpringStandardDialect());
engine.addDialect(new LayoutDialect());

engine.setTemplateResolver(resolver);
engine.setMessageResolver(new SpringMessageResolver());
engine.setMessageSource(messageSource);

WebContext ctx = new WebContext(request, response, context, pageModel.getLocale());
pageModel.getModel().keySet().forEach(it -> ctx.setVariable(it, pageModel.getModel().get(it)));

return engine.process(viewName, ctx);

这将呈现大多数模板,而不会出现任何问题,直到我在模板中使用以下内容为止:

<span th:text="${#calendars.format(#calendars.createForTimeZone(#calendars.year(listLastModifiedDate), #calendars.month(listLastModifiedDate), #calendars.day(listLastModifiedDate),'UTC'), 'yyyy-MMM-dd')}"></span>

我收到以下错误:

org.springframework.expression.spel.SpelEvaluationException: EL1004E: Method call: Method year(java.util.Date) cannot be found on type org.thymeleaf.expression.Calendars

模板未呈现。

好像是一个方言问题,可能我缺少一些方言,但我不知道是哪个方言?

2 个答案:

答案 0 :(得分:0)

#calendars没有方法year(java.util.Date)。它具有方法year​(Calendar target)。实用程序对象#dates确实具有方法year​(Date target)

这行吗?

<span th:text="${#calendars.format(#calendars.createForTimeZone(#dates.year(listLastModifiedDate), #dates.month(listLastModifiedDate), #dates.day(listLastModifiedDate),'UTC'), 'yyyy-MMM-dd')}" />

答案 1 :(得分:0)

问题出在配置中。我缺少适当的Thymeleaf环境。在WebContext ctx初始化之后添加此行解决了该问题:

ctx.setVariable( 
    ThymeleafEvaluationContext.THYMELEAF_EVALUATION_CONTEXT_CONTEXT_VARIABLE_NAME, 
    new ThymeleafEvaluationContext(applicationContext, new DefaultFormattingConversionService()));