spring整合thymeleaf
Posted wsfu
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了spring整合thymeleaf相关的知识,希望对你有一定的参考价值。
官方文档入口:https://www.thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html
1、首先需要引入thymeleaf的依赖(据官网文档,thymeleaf-spring3与thymeleaf-spring4用法基本一致)
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring4</artifactId> <version>3.0.11.RELEASE</version> </dependency>
2、配置thymeleaf的模板解析器、模板引擎与视图解析器
官方文档以xml作为配置方法,因为不是很方便,此处使用java方式进行配置
package com.example.demo.config; import javax.servlet.ServletContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.context.ServletContextAware; import org.thymeleaf.spring4.SpringTemplateEngine; import org.thymeleaf.spring4.view.ThymeleafViewResolver; import org.thymeleaf.templateresolver.ServletContextTemplateResolver; @ComponentScan(basePackages="com.example.demo.controller") public class ServletConfig implements ServletContextAware { private ServletContext servletContext; @Override public void setServletContext(ServletContext servletContext) { this.servletContext = servletContext; } /* 加载thymeleaf模板 */ @Bean public ServletContextTemplateResolver templateResolver() { ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(this.servletContext); resolver.setPrefix("/WEB-INF/templates/"); resolver.setSuffix(".html"); resolver.setTemplateMode(TemplateMode.HTML);
resolver.setCharacterEncoding("UTF-8");
resolver.setCacheable(true);
return resolver;
}
/* 模板引擎,渲染并返回结果 */ @Bean public SpringTemplateEngine templateEngine() { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver()); templateEngine.setEnableSpringELCompiler(true); return templateEngine; } /* 视图解析器 */ @Bean public ThymeleafViewResolver viewResolver() { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine()); viewResolver.setCharacterEncoding("UTF-8"); return viewResolver; } }
在配置模板解析器的时候,我选择的是实现ServletContextAware接口以获取ServletContext,并以此为参数创建ServletContextTemplateResolver。此处应该还可以用另一种方式进行配置:
public class WebConfig implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } //加载 Thymeleaf 模板 @Bean public SpringResourceTemplateResolver templateResolver() { SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver(); templateResolver.setApplicationContext(this.applicationContext); templateResolver.setPrefix("/WEB-INF/templates/"); templateResolver.setSuffix(".html");
return templateResolver; } }
3、测试:
@RequestMapping("/test") public String test(Model model) throws IOException { List<User> userList = baseService.queryUsers(); model.addAttribute("userList", userList); return "test"; }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>测试页面</title> </head> <body> <ul th:each="prop : ${userList}"> <li th:text="${prop.userName}"></li> </ul> </body> </html>
以上是关于spring整合thymeleaf的主要内容,如果未能解决你的问题,请参考以下文章
spring mvc 整合jsp和thymeleaf两个模板引擎
Spring MVC 3.2 Thymeleaf Ajax 片段