1、模板简单介绍
Thymeleaf是一个流行的模板引擎,该模板引擎采用Java语言开发;
模板引擎是一个技术名词,是跨领域跨平台的概念,在Java语言体系下有模板引擎,在C#、PHP语言体系下也有模板引擎,在JavaScript中也会用到模板引擎技术;
Java生态下的模板引擎有 Thymeleaf 、Freemaker、Velocity、Beetl(国产) 等;
Thymeleaf 它是基于HTML的,Thymeleaf模板本身也是一个html页面,Thymeleaf 要寄托在HTML的标签下实现对数据的展示;
Spring boot 集成了thymeleaf模板技术,并且spring boot官方也推荐使用thymeleaf来替代JSP技术;
Thymeleaf是另外的一种模板技术,它本身并不属于SpringBoot,SpringBoot只是很好地集成这种模板技术,作为前端页面的数据展示;
Thymeleaf旨在提供⼀个优雅的、⾼度可维护的创建模板的⽅式,为了实现这⼀⽬标,Thymeleaf建⽴在⾃然模板的概念上,将其逻辑注⼊到模板⽂件中,不会影响模板设计原型。 这改善了设计的沟通,弥合了设计和开发团队之间的差距。
Thymeleaf与JSP的区别在于,不运行项目之前,Thymeleaf也是纯HTML(不需要服务端的支持)而JSP需要进行一定的转换,这样就方便前端人员进行独立的设计、调试。
2、thymeleaf模板
我们直接进入代码:
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
User.java
package com.zrh.springboot01.entity;
import lombok.Data;
/**
* @author zrh
* @site IDEA项目
* @company
* @create 2019-11-0811:48
*/
@Data
public class User {
private String uid;
private String uname;
public User() {
}
public User(String uid, String uname) {
this.uid = uid;
this.uname = uname;
}
}
application.yml
server:
port: 80
servlet:
context-path: /
spring:
thymeleaf:
cache: false
ThymeleafControlle.java
package com.zrh.springboot01.controller;
import com.zrh.springboot01.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.ArrayList;
import java.util.List;
/**
* @author zrh
* @site IDEA项目
* @company
* @create 2019-11-0811:25
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafControlle {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView(