文章目录
一、SpringBoot整合Thymeleaf
1.1导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
1.2在application.properties文件添加配置
########## 配置thymeleaf ##########
spring.thymeleaf.cache=false
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.prefix=classpath:/templates
spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.servlet.content-type=text/html
1.3创建实体类
package com.qf.pojo;
import java.util.Date;
public class User {
private Integer id;
private String username;
private String password;
private Date birthday;
public User(Integer id, String username, String password, Date birthday) {
this.id = id;
this.username = username;
this.password = password;
this.birthday = birthday;
}
public User() {
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
", birthday=" + birthday +
'}';
}
}
1.4创建Controller
package com.qf.controller;
import com.qf.pojo.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;
import java.util.Date;
@Controller
@RequestMapping("user")
public class UserController {
@RequestMapping("findAll")
public String findAll(Model model) {
ArrayList<User> users = new ArrayList<>();
users.add(new User(1001, "张三", "123", new Date()));
users.add(new User(1002, "李四", "456", new Date()));
users.add(new User(1003, "王五", "789", new Date()));
model.addAttribute("users", users);
return "/list";
}
@RequestMapping("findById")
public String findById(Model model, String uid) {
System.out.println(uid);
if (uid.equals("1001")) {
User user = new User(1001, "张三", "123", new Date());
model.addAttribute("user", user);
}
return "/queryOne";
}
}
1.5在templates目录下创建list.html以及queryOne.html
list.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div th:if="${users!=null}">
<table border="1" width="600">
<tr th:each="user,state : ${users}">
<td th:text="${state.count}"></td>
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd HH:mm:ss')}"></td>
</tr>
</table>
<hr>
<table border="1" width="600">
<!-- 第二个变量,可以获取遍历的元素的状态-->
<tr th:each="user,state : ${users}">
<td th:text="${state.index}"></td>
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd HH:mm:ss')}"></td>
</tr>
</table>
<hr>
<table border="1" width="600">
<!--如果不设置表示状态的变量,默认遍历的元素的变量名+Stat
表示状态的变量-->
<tr th:each="user : ${users}">
<td th:text="${userStat.count}"></td>
<td th:text="${user.id}"></td>
<td th:text="${user.username}"></td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-
dd HH:mm:ss')}"></td>
</tr>
</table>
</div>
<hr color="red">
<table border="1" width="600">
<tr>
<th>序号</th>
<th>姓名</th>
<th>生日</th>
<th>详情</th>
</tr>
<tr th:each="user : ${users}">
<td th:text="${userStat.count}"></td>
<td th:text="${user.username}"></td>
<td th:text="${#dates.format(user.birthday, 'yyyy-MM-dd HH:mm:ss')}"></td>
<td><a th:href="@{/user/findById(uid=${user.id})}">查询</a></td>
</tr>
</table>
</body>
</html>
queryOne.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form th:object="${user}">
<!-- *{...}选择表达式一般跟在th:object后,直接选择object中的属性-->
<input type="hidden" th:id="*{id}" name="id">
用户名:<input type="text" th:value="*{username}" name="username" /><br /><br />
密码:<input type="text" th:value="*{password}" name="password"/>
</form>
</body>
</html>