thymeleaf模板:
是springboot官方推荐的使用html
相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
application的配置
#项目的路径
server:
servlet:
context-path: /springboot04
spring:
thymeleaf:
#设置不开启缓存
cache: false
在controller中的代码
@RequestMapping("/role/list")
public ModelAndView roleList(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("list");
modelAndView.addObject("name","老王");
modelAndView.addObject("sex","gril");
List list = new ArrayList();
list.add(new Role(1,"老师","传授知识"));
list.add(new Role(2,"学生","搞事情"));
modelAndView.addObject("roles",list);
return modelAndView;
}
html页面的代码
<!DOCTYPE html>
<!--导入thymeleaf(为了有提示方便代码编写)-->
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
用户列表
<h1 th:text="${title}">默认值</h1>
<table width="600px" border="1px">
<thead>
<tr>
<td>用户id</td>
<td>用户名</td>
<td>备注</td>
</tr>
<tr th:each="user : ${users}">
<td th:text="${user.uid}"></td>
<td th:text="${user.userName}"></td>
<td th:text="${user.desc}"></td>
</tr>
</thead>
</table>
<select>
<option th:each="user : ${users}" th:value="${user.uid}" th:text="${user.userName}"></option>
</select>
</body>
</html>
最终的效果