Thymeleaf模板是原生的,不依赖于标签库。它能在接受原始HTML的地方进行编辑和渲染。由于没有与Servlet规范耦合,因此Thymeleaf模板能够进入JSP所无法涉及的领域
如果想要在Spring MVC项目中使用Thymeleaf模板,那么只需要做到以下几步即可:
(1)配置使用Thymeleaf模板渲染Web视图:
在没有配置各种模板渲染技术之前,通常我们会这样配置:
1
2
3
4
因此现在首先需要注释掉上面的配置,同时使用JavaConfig来引入Thymeleaf模板渲染Web视图:
Java
package cn.zifangsky.config;
import javax.servlet.ServletContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;
/**
* 使用Thymeleaf模板渲染Web视图
* @author zifangsky
*
*/
@Configuration
public class TemplateConfig {
@Bean
public ServletContextTemplateResolver templateResolver(ServletContext servletContext){
ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver(servletContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
return templateResolver;
}
@Bean
public SpringTemplateEngine templateEngine(ServletContextTemplateResolver templateResolver){
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
return templateEngine;
}
@Bean
public ViewResolver viewResolver(SpringTemplateEngine templateEngine){
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setCharacterEncoding("UTF-8");
return viewResolver;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49packagecn.zifangsky.config;
importjavax.servlet.ServletContext;
importorg.springframework.context.annotation.Bean;
importorg.springframework.context.annotation.Configuration;
importorg.springframework.web.servlet.ViewResolver;
importorg.thymeleaf.spring4.SpringTemplateEngine;
importorg.thymeleaf.spring4.view.ThymeleafViewResolver;
importorg.thymeleaf.templateresolver.ServletContextTemplateResolver;
/**
* 使用Thymeleaf模板渲染Web视图
* @author zifangsky
*
*/
@Configuration
publicclassTemplateConfig{
@Bean
publicServletContextTemplateResolvertemplateResolver(ServletContextservletContext){
ServletContextTemplateResolvertemplateResolver=newServletContextTemplateResolver(servletContext);
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
returntemplateResolver;
}
@Bean
publicSpringTemplateEnginetemplateEngine(ServletContextTemplateResolvertemplateResolver){
SpringTemplateEnginetemplateEngine=newSpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver);
returntemplateEngine;
}
@Bean
publicViewResolverviewResolver(SpringTemplateEnginetemplateEngine){
ThymeleafViewResolverviewResolver=newThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine);
viewResolver.setCharacterEncoding("UTF-8");
returnviewResolver;
}
}
注:这里还需要在Spring MVC的配置文件中配置自动扫描上面的JavaConfig所在的包,也就是:
XHTML
1
(2)定义一个简单模板home.html:
从上面的配置可知,需要将模板文件放置到/WebContent/WEB-INF/templates/home.html路径下,其内容如下:
XHTML
标题
Hello
The year is 1949.
1
2
3
4
5
6
7
8
9
10
11
12
13
标题
Hello
The year is1949.