无法在 Thymeleaf / Spring4 中使用表达式

Unable to use expressions in Thymeleaf / Spring4

使用过 Tiles 后,我想尝试使用 Thymeleaf 模板,但是,我似乎根本无法在 HTML 页面中使用任何表达式。

我试过使用项目启动器使用快速启动 Spring MVC,在渲染到 jsp 时一切正常,但没有将 HTML 与 Thymeleaf 一起使用 - HTML 显示页面,因此它显然在一定程度上起作用,但所有表达式都显示为简单文本且未计算。

这是 servlet-context.xml 的 Thymeleaf 部分:

<beans:bean id="templateResolver"
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <beans:property name="prefix" value="/WEB-INF/templates/" />
    <beans:property name="suffix" value=".html" />
    <beans:property name="templateMode" value="HTML5" />
</beans:bean>
<beans:bean id="templateEngine" class="org.thymeleaf.spring4.SpringTemplateEngine">
    <beans:property name="templateResolver" ref="templateResolver" />
    <beans:property name="additionalDialects">
        <beans:set>
        <beans:bean class="nz.net.ultraq.thymeleaf.LayoutDialect" />
        </beans:set>
    </beans:property>
</beans:bean>
<beans:bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <beans:property name="templateEngine" ref="templateEngine" />
</beans:bean>

控制器只是从快速启动 MVC 模板生成的控制器:

@Controller
public class HomeController {

@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Locale locale, Model model) {        
    Date date = new Date();
    DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);

    String formattedDate = dateFormat.format(date);
    model.addAttribute("serverTime", formattedDate );
    return "home";
    }
}

HTML 只是从 jsp 复制而来,带有额外的 Thymleaf 表达式:

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1"/>
<title>Insert title here</title>
</head>
<body>
<p>  (HTML)The time on the server is ${serverTime}. </p>
#{serverTime}

<h1>th:text:="#{serverTime}"</h1>

</body>
</html>

关于我在这里遗漏的任何想法?谢谢

问题与您的 html 有关,Thymeleaf 使用标准方言定义 html 以 th:

开头的属性

您的 html body 应如下所示:

<body>
<p>The time on the server is <span th:text="${serverTime}">time</span>.</p>

<h1 th:text="${serverTime}">Time again!</h1>

</body>

我建议您阅读教程“Using Thymeleaf