SpringBoot——模板引擎(七)

简介

Spring MVC 支持各种模版技术,如 Thymeleaf , FreeMarker , JSP 等。而Thyemeleaf 原型即页面的特性或许更符合 Spring Boot 快速开发的思想而被官方推荐。

Thymeleaf 是适用于 Web 开发的服务端 Java 模版引擎,Thymeleaf 为开发工作流程带来优雅自然的模版,由于其非侵入的特性,可以让页面不管是在静态原型下还是用作模版引擎时都有良好的页面展现。

引入Thymeleaf

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

yml配置

1
2
3
4
5
6
7
8
spring:
#模板引擎
thymeleaf:
encoding: utf-8
servlet:
content-type: text/html
prefix: classpath:/html/
suffix: .html

html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:text="${hello}">这里是原来的文本!</div>
<hr/>
<!-- 不转义 -->
<div th:utext="${hello}"></div>
<hr/>
<h4 th:each="user : ${users}" th:text="${user}"></h4>
<hr/>

<h4>
<span th:each="user : ${users}"> [[${user}]] </span>
</h4>
</body>
</html>

Controller

1
2
3
4
5
6
@RequestMapping("/success")
public String success(ModelMap model) {
model.addAttribute("hello", "<h1>Hello World</h1>");
model.addAttribute("users", Arrays.asList("zhangsan", "lisi", "wangwu"));
return "test";
}

测试

SpringBoot——模板引擎(七)_2020-05-18-15-59-21.png

不兼容

当在使用Thymeleaf,实际上在用 ThymeleafViewResolver,这时mvc 的 InternalResourceViewResolver 不生效,当然我们可以通过配置让它们可以兼容使用。

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
49
50
51
52
53
@Configuration
@EnableWebMvc
@ComponentScan
public class WebConfig extends WebMvcConfigurerAdapter {

@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/");
resolver.setSuffix(".jsp");
resolver.setViewNames("jsp/*");
resolver.setOrder(2);
return resolver;
}

@Bean
public ITemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setTemplateMode("HTML5");
templateResolver.setPrefix("/WEB-INF/");
templateResolver.setSuffix(".html");
templateResolver.setCharacterEncoding("utf-8");
templateResolver.setCacheable(false);
return templateResolver;
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.setTemplateResolver(templateResolver());
return templateEngine;
}

@Bean
public ThymeleafViewResolver viewResolverThymeLeaf() {
ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
viewResolver.setTemplateEngine(templateEngine());
viewResolver.setCharacterEncoding("utf-8");
viewResolver.setViewNames(new String[]{"thymeleaf/*"});
viewResolver.setOrder(1);
return viewResolver;
}

@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
super.addResourceHandlers(registry);
}
}
文章目录
  1. 1. 简介
  2. 2. 引入Thymeleaf
  3. 3. yml配置
  4. 4. html
  5. 5. Controller
  6. 6. 测试
  7. 7. 不兼容
|