SpringBoot–基础–03–thymeleaf模板引擎
一、代码结构
HelloController
@Controller
public class HelloController {
@RequestMapping("/success")
public String success(Map<String, Object> map) {
map.put("hello", "通过模板引擎跳转到success.html页面");
return "success";
}
}
success.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>成功!</h1>
<div th:text="${hello}"></div>
</body>
</html>
二、配置依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
三、自动配置代码
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
public static final String DEFAULT_PREFIX = "classpath:/templates/";//前缀
public static final String DEFAULT_SUFFIX = ".html";//后缀
- 只要我们把HTML页面放在classpath:/templates/下,thymeleaf就能自动渲染,类似于Spring的视图解析器
- 可以根据实际情况,通过application.yml去修改配置
四、测试
代码位置
https://gitee.com/DanShenGuiZu/learnDemo/tree/mysql_mybaties_DB/springboot-learn/springboot-2