springboot_模板引擎
一、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
案例一(如何获取单个字符串):
User
package com.yuan.springboot01.entity;
import lombok.Data;
import lombok.ToString;
/**
* @author 梦园
* @site www.mengyuan.com
* @company xxx
* @create 2020-11-29-8:31
*/
@Data
public class User {
private Integer uid;
private String uname;
private String pwd;
public User(Integer uid, String uname, String pwd) {
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
}
UserController
package com.yuan.springboot01.controller;
import com.yuan.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 梦园
* @site www.mengyuan.com
* @company xxx
* @create 2020-11-29-8:32
*/
@Controller
@RequestMapping("/thymeleaf")
public class UserController {
@RequestMapping("/list")
public String hello(HttpServletRequest request){
/**
* 1.获取单个值
* 2.能够在html页面进行遍历展示
* 3.如何在html页面转义html代码块
*/
request.setAttribute("msg","传输单个字符串!!");
List<User> userList=new ArrayList<>();
userList.add(new User(1,"zs","123"));
userList.add(new User(2,"ls","234"));
userList.add(new User(3,"ww","456"));
request.setAttribute("userList",userList);
request.setAttribute("htmlStr","<span style='color:red'>转义html代码块</span>");
return "list";
}
}