Srping 4.0 + Thymeleaf 3 进行整合
前言
最近有需求要用到 Thymeleaf
模板引擎,所以我就学习了一下如何使用这个东西。
配置过程(Maven版)
配置过程三步走:
&& 添加 Thymeleaf
的核心依赖和相关 spring
的版本依赖
<!-- 核心依赖 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<!-- 我这里用了 spring 4 -->
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring4</artifactId>
<version>3.0.0.RELEASE</version>
</dependency>
<!-- Provided dependencies 这个是接下来启动ServletContext 的时候用到-->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>7.0</version>
<scope>provided</scope>
</dependency>
&& 创建两个类
在自己的项目目录下添加一个 config
文件夹,里面创建两个类: SpringWebInitializer.java
和 ThymeleafConfig.java
ThymeleafConfig.java
mport org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.ITemplateResolver;
/**
*
* @author tanjiancheng
* @date 17/12/15
*/
@Configuration
@EnableWebMvc
@ComponentScan("com.ipeak.crm.configuration")
public class ThymeleafConfig extends WebMvcConfigurerAdapter implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
@Bean
public ViewResolver viewResolver() {
ThymeleafViewResolver resolver = new ThymeleafViewResolver();
resolver.setTemplateEngine(templateEngine());
resolver.setCharacterEncoding("UTF-8");
return resolver;
}
@Bean
public TemplateEngine templateEngine() {
SpringTemplateEngine engine = new SpringTemplateEngine();
engine.setEnableSpringELCompiler(true);
engine.setTemplateResolver(templateResolver());
return engine;
}
private ITemplateResolver templateResolver() {
SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
resolver.setApplicationContext(applicationContext);
resolver.setPrefix("/WEB-INF/templates/");
resolver.setTemplateMode(TemplateMode.HTML);
return resolver;
}
SpringWebInitializer.java
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration.Dynamic;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
/**
* 使用配置的方法将 spring 进行启动 (代替了 web.xml)
* @author tanjiancheng
*/
public class SpringWebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
//注解配置 ApplicationContext
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
//将相关配置文件加载 ThymeleafConfig
context.register(ThymeleafConfig.class);
context.setServletContext(servletContext);
// Spring MVC front controller
Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(context));
//映射
servlet.addMapping("/");
//设置启动书序
servlet.setLoadOnStartup(1);
}
}
&& 书写测试类并启动 tomcat
在 Controller
包下创建 IndexController.java
IndexController.java
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
/**
* @author tanjiancheng
*/
@Controller
public class IndexController {
@RequestMapping(value = "/IndexController", method = RequestMethod.GET)
public String index(Model model) {
model.addAttribute("recipient", "World");
return "index.html";
}
}
参考文献
Thymeleaf 3 ten-minute migration guide
Hello World example with Thymeleaf 3 and Spring 4(GitHub)