thymeleaf
1> 相关pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
注意:
Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
spring.thymeleaf.cache=false
正式环境还是要将缓存开启的
2>修改配置文件application.yml
spring:
thymeleaf:
cache: false
3>代码展示
html页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title th:text="${title}"></title>
</head>
<body>
<h2 th:text="${welcome}"></h2>
<h2>数据展示</h2>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
</tr>
</thead>
<tbody>
<tr th:each =" user : ${users}">
<th th:text="${user.getUid()}">编号</th>
<th th:text="${user.getUsername()}">姓名</th>
<th th:text="${user.getSex()}">性别</th>
</tr>
</tbody>
</table>
<h3>
<select>
<option th:each="user : ${users}" th:value="${user.getUid()}" th:text="${user.getUsername()}"></option>
</select>
</h3>
</body>
</html>
实体类
public class User {
private String uid;
private String username;
private String sex;
public User() {
}
public User(String uid, String username, String sex) {
this.uid = uid;
this.username = username;
this.sex = sex;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
controller层
@Controller
public class UserController {
@RequestMapping("/user")
public ModelAndView user(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("user");
modelAndView.addObject("title","用户页面");
modelAndView.addObject("welcome","欢迎来到用户列表界面");
List<User> list = new ArrayList<>();
list.add(new User("1","wangwangzaijiao","girl"));
list.add(new User("2","taozi","girl"));
list.add(new User("3","wangwangzaijiaotaozi","buxiaode"));
modelAndView.addObject("users",list);
return modelAndView;
}
}
效果图
freemarker
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
application.yml文件的默认配置
spring:
freemarker:
# 设置模板后缀名
suffix: .ftl
# 设置文档类型
content-type: text/html
# 设置页面编码格式
charset: UTF-8
# 设置页面缓存
cache: false
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/role
mvc:
static-path-pattern: /static/**
页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>取一个好听的名字</title>
</head>
<body>
<h2>取值</h2>
取一个好听的名字,叫(${name!})
<h2>exits非空判断</h2>
<#if name?exists>
123
</#if>
<h2>if条件判断结构</h2>
<#if name=='好听'>
好听的名字
<#elseif name=='不好听'>
不好听
<#else >
名字未知
</#if>
<h2>循环</h2>
<table>
<thead>
<tr>
<th>编号</th>
<th>名称</th>
<th>备注</th>
</tr>
</thead>
<tbody>
<#list roles as role>
<tr>
<th>${role.rid}</th>
<th>${role.roleName}</th>
<th>${role.remark}</th>
</tr>
</#list>
</tbody>
</table>
<h2>全局变量,局部变量</h2>
<#assign ctx1>
${springMacroRequestContext.contextPath}
</#assign>
<#global ctx2>
${springMacroRequestContext.contextPath}
</#global>
${ctx1},
</br>
${ctx2}
<h2>include</h2>
<#include "head.ftl">
</body>
</html>
controller
@RequestMapping("/role/role")
public ModelAndView role(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("role");
//"好听"
modelAndView.addObject("name","好听");
List<Role> list = new ArrayList<>();
list.add(new Role("1","wangwangzaijiao","nizhidaoma"));
list.add(new Role("2","taozi","buzhidaolei"));
list.add(new Role("3","wangwangzaijiaotaozi","nawoyebuzhidaole"));
modelAndView.addObject("roles",list);
return modelAndView;
}
role
public class Role {
private String rid;
private String roleName;
private String remark;
public Role() {
}
public Role(String rid, String roleName, String remark) {
this.rid = rid;
this.roleName = roleName;
this.remark = remark;
}
public String getRid() {
return rid;
}
public void setRid(String rid) {
this.rid = rid;
}
public String getRoleName() {
return roleName;
}
public void setRoleName(String roleName) {
this.roleName = roleName;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
}