Thymeleaf模板
1.相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.Spring Boot官方文档建议在开发时将缓存关闭,那就在application.properties文件中加入下面这行
注:在一般开发中,我们习惯于将application.properties更换为application.yml,因为这种的话,在application中写代码会自动产生层级结构,更加容易看清
server:
port: 80
servlet:
context-path: /
# 关闭缓存thymeleaf,等待正式运行时开启
spring:
thymeleaf:
cache: false
3.为了前台代码使用方便,我们将加入下面这行代码:
<caption th:utext="${a}"></caption>
4.新建一个user类:
package com.swx.springboot01.entity;
import lombok.Data;
/**
* @author Songwanxi
* @site www.lentter.club
* @company
* @create 2019-12-27 20:22
*/
@Data
public class User {
private Integer uid;
private String uname;
private String upwd;
public User(Integer uid, String uname, String upwd) {
this.uid = uid;
this.uname = uname;
this.upwd = upwd;
}
public User() {
}
}
5.新建ThymeleafController类:
package com.swx.springboot01.controller;
import com.swx.springboot01.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 Songwanxi
* @site www.lentter.club
* @company
* @create 2019-12-27 20:23
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
mv.setViewName("/list");
List<User> list = new ArrayList();
list.add(new User(1,"张三","123"));
list.add(new User(2,"李四","456"));
mv.addObject("userList",list);
mv.addObject("msg","员工列表");
//转义
mv.addObject("a","<span style='color:red'>优秀员工</span>");
return mv;
}
}
6.前台代码:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="600px">
<caption th:text="${msg}"></caption>
<th>员工id</th>
<th>员工名</th>
<th>员工密码</th>
<tr th:each=" u : ${userList}">
<td th:text="${u.uid}"></td>
<td th:text="${u.uname}"></td>
<td th:text="${u.upwd}"></td>
</tr>
</table>
<h2 th:utext="${a}"></h2>
</body>
</html>
实际效果:
Freemarker模板
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!--可以不加,但是做项目的时候可能会用-->
<resources>
<!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!--freemarker模板也读取需要注释标红地方-->
<resource>
<directory>src/main/resources</directory>
<includes>
<!--<include>*.properties</include>-->
<!--<include>*.xml</include>-->
<!--<include>*.yml</include>-->
</includes>
</resource>
</resources>
添加配置文件:application.yml,这里为了我们演示方便,就加上了项目名:
server:
port: 80
servlet:
context-path: /index
spring:
thymeleaf:
cache: false
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/**
写一个role类,拿来写假数据:
package com.swx.springboot01.entity;
import lombok.Data;
/**
* @author Songwanxi
* @site www.lentter.club
* @company
* @create 2019-12-28 14:35
*/
@Data
public class Role {
private Integer rid;
private String rname;
private String rpwd;
public Role(Integer rid, String rname, String rpwd) {
this.rid = rid;
this.rname = rname;
this.rpwd = rpwd;
}
public Role() {
}
}
FreemarkerController
package com.swx.springboot01.controller;
import com.swx.springboot01.entity.Role;
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 Songwanxi
* @site www.lentter.club
* @company
* @create 2019-12-28 14:24
*/
@Controller
@RequestMapping("/free")
public class FreemarkerController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
mv.setViewName("flist");
mv.addObject("name","漩涡鸣人");
mv.addObject("sex","gay");
List<Role> list = new ArrayList<>();
list.add(new Role(1,"zs","123"));
list.add(new Role(2,"zs1","456"));
list.add(new Role(3,"zs2","786"));
mv.addObject("roles",list);
return mv;
}
}
前台flist:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>freemarker</title>
</head>
<body>
<h2>取值</h2>
<h3>提供默认值</h3>
welcome 【${name!'未知'}】 to freemarker!
<h3>exists用在逻辑判断</h3>
<#if name?exists>
${name}
</#if>
<h2>条件</h2>
<#if sex=='girl'>
女
<#elseif sex=='boy'>
男
<#else>
保密
</#if>
<h2>循环</h2>
<table border="1px" width="600px">
<thead>
<tr>
<td>ID</td>
<td>角色名</td>
<td>描述</td>
</tr>
</thead>
<tbody>
<#list roles as role>
<tr>
<td>${role.rid}</td>
<td>${role.rname}</td>
<td>${role.rpwd}</td>
</tr>
</#list>
</tbody>
</table>
<h2>include</h2>
<#include 'foot.ftl'>
<h2>局部变量(assign)/全局变量(global)</h2>
<#assign ctx1>
${springMacroRequestContext.contextPath}
</#assign>
<#global ctx2>
${springMacroRequestContext.contextPath}
</#global>
${ctx1}和${ctx2}
</body>
</html>
底部foot:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>底部</title>
</head>
<body>
这是底部信息
</body>
</html>
测试效果:
Thymeleaf包含公共区域的两种方案:
公共区域common.html
<div th:fragment="d1">第一部分</div>
<div th:fragment="d2">第二部分</div>
<div th:fragment="d3">第三部分</div>
修改list.html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<table border="1" cellpadding="0" cellspacing="0" width="600px">
<caption th:text="${msg}"></caption>
<th>员工id</th>
<th>员工名</th>
<th>员工密码</th>
<tr th:each=" u : ${userList}">
<td th:text="${u.uid}"></td>
<td th:text="${u.uname}"></td>
<td th:text="${u.upwd}"></td>
</tr>
</table>
<h2 th:utext="${a}"></h2>
<!--thymeleaf包含公共区域的方案-->
<h2>包含整个th:include</h2>
<div th:include="common.html"></div>
<h2>包含部分th:replace</h2>
<div th:replace="common :: d2"></div>
</body>
</html>
显示结果: