Thymeleaf模板
关于Thymeleaf的优点,只说一条:它就是html页面
导入pom依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
Spring Boot官方文档建议在开发时将缓存关闭
为了代码有层次 application.properties 的后缀改成了application.yml
那就在 application.yml 文件中加入下面这行
后台相应代码
User.java
package com.lzy.springboot01.entity;
import lombok.Data;
@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() {
}
}
新建ThymeleafController类:
package com.lzy.springboot01.controller;
import com.lzy.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;
@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;
}
}
前台代码:
<!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模板
学习网站: http://freemarker.foofun.cn/
导入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>
右键new一开始是没有freemarker这个类
有两种方法
一是创建一个html文件直接改后缀名为ftl
二如下图
application.yml文件的默认配置
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/**
list.ftl
<!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.rdemo}</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.ftl
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>调用界面</title>
</head>
<body>
</body>
</html>
RoleController.java
package com.lzy.springboot1.controller;
import com.lzy.springboot1.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;
/**
* 介绍freemarker模块引擎的应用
*/
@Controller
@RequestMapping("/freemarker")
public class RoleController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView m=new ModelAndView();
m.setViewName("list");
m.addObject("name","学生列表");
m.addObject("sex","boy");
List<Role> roles=new ArrayList<>();
roles.add(new Role(1,"网管","网吧总管"));
roles.add(new Role(2,"包租婆","管水管电管房租"));
roles.add(new Role(3,"总管","公司老总"));
m.addObject("roles",roles);
return m;
}
}
注意点:
1、application.yml中可以配置模板存放位置的根路径、以及静态资源文件存放位置的根路径
2、${springMacroRequestContext.contextPath}:SpringBoot中获取项目名
3、不推荐使用全局变量。即便它们属于不同的命名空间, 全局变量也被所有模板共享,因为它们是被 import进来的。
thymeleaf中替代jsp:include的写法
两种情况
一种是应用一段html代码
在需要应用的地方添加
<!-- thymeleaf 包含公共区域的方案-->
<div th:replace="common :: h2"></div>
一种是包含整个html页面
<div th:include="freemarker/common/head"></div>