我是第一次练习thymeleaf模板引擎。我已经按照教程等进行了操作,但我不知道哪里出了问题。
我的 Controller :
public String mainPage(Model model){
model.addAttribute("data", "Hello Thymeleaf");
return "main";
}
我的html如下:
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>th:text="${data}"</h1>
</body>
</html>
当我点击 localhost 时,它显示 th:text="${data}"而不是 Hello Thymeleaf
<h1>"${data}"</h1>
也不起作用。 View 解析器配置必须正确,因为它将 main 解析为 main.html。我正在使用 spring4 SpringTemplateEngine 和 spring4 thymeleaf View 解析器。
提前致谢
最佳答案
您必须使用th:text
<h1 th:text="${data}"></h1>
或者如果你不想使用 th:text
属性,那么你必须使用 th:inline="text"
并使 thymeleaf 渲染标签内的上下文。但请确保将变量放入 [[
和 ]]
<h1 th:inline="text">[[${data}]]</h1>
关于java - Thymeleaf 不显示 Spring Controller 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29569682/