相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
对应的后台代码
package com.zking.springboot02.entity;
/**
* @author TYQ
* @site www.xiaomage.com
* @company xxx公司
* @create 2019-02-18 16:48
*/
public class User {
private Integer uid;
private String usename;
private String password;
public User() {
}
public User(Integer uid, String usename, String password) {
this.uid = uid;
this.usename = usename;
this.password = password;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public String getUsename() {
return usename;
}
public void setUsename(String usename) {
this.usename = usename;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.zking.springboot02.controller;
import com.zking.springboot02.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 TYQ
* @site www.xiaomage.com
* @company xxx公司
* @create 2019-02-18 16:20
*/
@Controller
public class IndexController {
@RequestMapping("/user/list")
public ModelAndView userlist(){
ModelAndView modelAndView=new ModelAndView();
List<User> list=new ArrayList<>();
list.add(new User(1,"zs","123"));
list.add(new User(2,"ls","123"));
modelAndView.addObject("users",list);
modelAndView.setViewName("user/list");
return modelAndView ;
}
}
前台HTML页面
list.html相关代码
<!DOCTYPE html >
<html xmlns:th="http://www.thymeleaf.org">
<html>
<head>
<meta charset="UTF-8">
<title>用户信息</title>
</head>
<body>
<table border="1" width="800px">
<thead bgcolor="aqua">
<tr>
<td>id</td>
<td>名字</td>
<td>密码</td>
</tr>
</thead>
<tbody>
<tr th:each="user:${users}">
<td th:text="${user.uid}">id</td>
<td th:text="${user.usename}">名字</td>
<td th:text="${user.password}">密码</td>
</tr>
</tbody>
</table>
<select style="width: 100px" >
<option th:each="user :${users}" th:value="${user.uid}" th:text="${user.usename}"></option>
</select>
</body>
</html>
注意:一定要记得把标签导进来 <html xmlns:th="http://www.thymeleaf.org">