我在Spring中有简单的doGet方法:
@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession(true);
List<Category> categories = categoriesService.getCategories();
req.setAttribute("categories", categories);
if (!resp.isCommitted()) {
req.getRequestDispatcher("thymeleaf_views/categories.html").forward(req, resp);
}
}
我可以获得HTML页面。但它的所有内容都没有得到解决((
<body>
<div>
<h2>Categories</h2>
<ul id="menu" style="list-style-type:none">
<li th:each="category: ${categories}" th:text="${category.name}"></li>
</ul>
</div>
</body>
当我尝试加载页面时 - 它按原样加载标签 - 不会用标签替换标签((可能是什么原因?
答案 0 :(得分:0)
您必须设置TemplateResolver。 E. g。喜欢以下方式:
@Configuration
public class ThymeleafConfig {
@Bean
public ServletContextTemplateResolver templateResolver() {
ServletContextTemplateResolver resolver = new ServletContextTemplateResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".html");
resolver.setTemplateMode("HTML5");
return resolver;
}
@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setTemplateResolver(templateResolver());
return engine;
}
@Bean
public ThymeleafViewResolver thymeleafViewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
return resolver;
}
}