1.模板引擎作用
2.ThymeLeaf的使用:
- 在SpringBoot框架中引入ThymeLeaf依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 改修ThymeLeaf配置项
查看自动配置org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties类的属性部分源码
@ConfigurationProperties(
prefix = "spring.thymeleaf"
)
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING;
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
private boolean checkTemplate = true;
private boolean checkTemplateLocation = true;
private String prefix = "classpath:/templates/";
private String suffix = ".html";
private String mode = "HTML";
private Charset encoding;
private boolean cache;
private Integer templateResolverOrder;
private String[] viewNames;
private String[] excludedViewNames;
private boolean enableSpringElCompiler;
private boolean renderHiddenMarkersBeforeCheckboxes;
private boolean enabled;
private final ThymeleafProperties.Servlet servlet;
private final ThymeleafProperties.Reactive reactive;
}
可知默认模板位置为: “classpath:/templates/”
上述配置可以在Spring Boot的配置文件application.properties中修改,示例:
# 修改thymeleaf模板存放位置
spring.thymeleaf.prefix="classpath:/static/";
# 模板文件后缀名
spring.thymeleaf.suffix=".html"
- 在html中的使用语法示例
controller类代码:
package com.mjj.springinitializrdemo.controller;
import com.mjj.springinitializrdemo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Controller
public class HelloController {
@RequestMapping("/hello")
public String sayHello(Model model, HttpSession session){
Student stu = new Student("majunjie",17, "0");
model.addAttribute("student",stu);
return "student"; //不加@ResponseBody时,该返回值对应的是模板文件的文件名(不带后缀)
}
@RequestMapping("/list")
public String getList(Model model){
List<Student> students = new ArrayList<>();
students.add(new Student("张三",12,"0",new Date()));
students.add(new Student("李四",13,"1",new Date()));
students.add(new Student("王五",14,"1",new Date()));
students.add(new Student("赵六",15,"0",new Date()));
model.addAttribute("students",students);
return "list";
}
}
student.html代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"> <!--添加thymeleaf的命名空间-->
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!-- thymeleaf内置对象的使用 比如#session 以及工具类对象的使用 比如#strings
具体工具类和内置对象列表可查阅官网-->
<h1 th:text="${#strings.append('欢迎您,',#session.getId())}"></h1>
<h1 th:text="${'欢迎您,'+#session.getId()}"></h1> <!-- 两种字符串拼接方法-->
<hr/>
姓名:<p th:text="${student.stuNam}" >张三</p> <!--属性的取值-->
年龄:<p >[[${student.age}]]</p> <!--行内表达式-->
性别:<p th:if="${student.sex} == 1">男</p> <!--条件表达式-->
<p th:if="${student.sex} == 0">女</p>
</body>
</html>
list.html代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>生日</th>
</tr>
<!--遍历语法-->
<tr th:each="u : ${students}">
<td>[[${u.stuNam}]]</td>
<td>[[${u.age}]]</td>
<td th:if="${#strings.equals(u.sex,'0')}">男</td>
<td th:if="${#strings.equals(u.sex,'1')}">女</td>
<td th:text="${#dates.format(u.birthday,'yyyy-MM-dd')}"></td>
</tr>
</table>
</body>
</html>
效果截图: