thymeleaf,freemarker模板
thymeleaf模板
关于Thymeleaf的优点,我只说一条:它就是html页面。下面直接上代码
相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
正式环境还是要将缓存开启的
<html xmlns:th="http://www.thymeleaf.org">
th:text
<tr th:each="user : ${users}">
<td th:text="${user.uid}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.desc}"></td>
</tr>
th:value
案例
application.yml
server:
servlet:
context-path: /
port: 80
user:
uname: zs
pwd: 123456
age: 22
sex: nan
addr: beijing
spring:
thymeleaf:
cache: false
User
package com.tang.springboot01.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;
/**
* @author CandyTang
* @create 2020-11-30 19:25
*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@ToString
public class User {
private Integer uid;
private String uname;
private String pwd;
}
UserController
package com.tang.springboot01.controller;
import com.tang.springboot01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* @author CandyTang
* @create 2020-11-30 19:27
*/
@Controller
@RequestMapping("/thymeleaf")
public class UserController {
@RequestMapping("/list")
public String hello(HttpServletRequest request){
/*
* 1、获取单个值
* 2、能够在HTML页面进行遍历展示
* 3、如何在HTML页面转义HTML代码块
* */
request.setAttribute("msg"