Spring Boot之引入模板引擎thymeleaf
一、动态资源:jsp(spring boot默认不支持)
推荐:模板引擎 thymeleaf
网页= 模板+数据
引入thymeleaf依赖:
<!-- 引入thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
怎么使用thymeleaf呢?
打开thymeleaf官网。
点击DOCs->

通过ThymeleafProperties源码得知:
使用thymeleaf只需要将文件放入目录:“classpath:/templates”,文件后缀为.html。
在templates文件中创建html文件。
在pdf中打开using text。

在result.html文件中添加代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<p th:text="${welcome}">Welcome to thymeleaf...!</p>
</body>
</html>
在控制器中添加:
@RequestMapping("welcome")
public String welcome(Map<String,Object> map){
map.put("welcome", "welcomeThymeleaf");//给request域中放入welcome
//给thymeleaf 准备数据
return "result";
}
注意:在以前传统的web项目中:静态资源修改后,不需要重启,但是在springboot中,修改后必须要重启。
th可以替换标签的原有值,运行之后,welcomeThymeleaf 代替了 welcome to thymeleaf

浙公网安备 33010602011771号