Thymeleaf简介
第一个模板页
修改配置
spring:
thymeleaf:
cache: false #开发时关闭缓存,不然没法看到实时页面
encoding: UTF-8
servlet:
content-type: text/html
package com.example.springboot_thymeleaf.controller;
import com.example.springboot_thymeleaf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class MyController {
@RequestMapping(value = {"","index"},method = RequestMethod.GET)
public String index(Model model){
User user=new User();
user.setUsername("lisi");
model.addAttribute("user",user);
return "index";
}
}
package com.example.springboot_thymeleaf.entity;
import java.io.Serializable;
public class User implements Serializable {
private String username;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
再templates包下创建index.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<span th:text="${user.username}">李四</span>
</body>
</html>
运行启动类
打开浏览器,输入网址
localhost:8080
即可显示lisi
但是这个模块是动静结合,刚才只是显示了动态页面,静态页面了?
我们可以到index.html文件所在位置,直接打开,就会显示李四