Springboot模板(二)
1、thymeleaf模板
thymeleaf以html结尾它本身自带页面缓存功能
pop依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
让thymeleaf缓存不默认开启在项目上线的时候要开启可以加快速度
spring:
thymeleaf:
cache: false
ThymeleafController
package com.xyx.springboot1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
/**
* @author xyx
* @site www.xyxmage.com
* @company xxx公司
* @create 2019-11-08 11:05
* RequestMapping("/thymeleaf")划分工作区间
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/list")
// ModelAndView 和String 同等意义返回一个页面 是一个核心的处理页面
public ModelAndView list(){
ModelAndView mv = new ModelAndView();//模型你要传的值视图返回的页面
// 模拟数据
mv.addObject("name","zs");//模型你要传的值
mv.setViewName("list");//地址 视图返回的页面
return mv;
}
//同上意义相同
// public String list2(HttpServletRequest request){
// return "list";
// }
}
前台HTML页面
<html xmlns:th="http://www.thymeleaf.org">
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thymeleaf页面</title>
</head>
<body>
Thymeleaf页面
<h2>显示文本</h2>
<span th:text="${name}"></span>
</body>
</html>

User
package com.xyx.springboot1.entity;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author xyx
* @site www.xyxmage.com
* @company xxx公司
* @create 2019-11-07 17:10
*/
@Data
public class User {
private String uid;
private String uname;
public User(String uid,String uname){
this.uid = uid;
this.uname = uname;
}
public User(){
}
}
ThymeleafController
package com.xyx.springboot1.controller;
import com.xyx.springboot1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* @author xyx
* @site www.xyxmage.com
* @company xxx公司
* @create 2019-11-08 11:05
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();//模型你要传的值视图返回的页面
List list = new ArrayList();
list.add(new User("1","zs"));
list.add(new User("2","ls"));
list.add(new User("3","ww"));
mv.addObject("userList",list);
mv.addObject("name","zs");//模型你要传的值
mv.setViewName("list");//地址 视图返回的页面
return mv;
}
//同上意义相同
// public String list2(HttpServletRequest request){
// return "list";
// }
}
循环
<h2>循环</h2>
<table>
<tr>
<td>用户id</td>
<td>用户名</td>
</tr>
<tr th:each="u : ${userList}">
<td th:text="${u.uid}">用户id</td>
<td th:text="${u.uname}">用户id</td>
</tr>
</table>

加入
mv.addObject("msg","<span style='color:red;'>双十一剁手活动即将开始</span>");
ThymeleafController
package com.xyx.springboot1.controller;
import com.xyx.springboot1.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
/**
* @author xyx
* @site www.xyxmage.com
* @company xxx公司
* @create 2019-11-08 11:05
*/
@Controller
@RequestMapping("/thymeleaf")
public class ThymeleafController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();//模型你要传的值视图返回的页面
List list = new ArrayList();
list.add(new User("1","zs"));
list.add(new User("2","ls"));
list.add(new User("3","ww"));
mv.addObject("userList",list);
mv.addObject("name","zs");//模型你要传的值
mv.addObject("msg","<span style='color:red;'>双十一剁手活动即将开始</span>");
mv.setViewName("list");//地址 视图返回的页面
return mv;
}
//同上意义相同
// public String list2(HttpServletRequest request){
// return "list";
// }
}
text.html
<h2>显示html</h2>
<div th:utext="${msg}"></div>
text.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thymeleaf页面</title>
</head>
<body>
Thymeleaf页面
<h2>显示文本</h2>
<span th:text="${name}"></span>
<h2>显示html</h2>
<div th:utext="${msg}"></div>
<h2>循环</h2>
<table>
<tr>
<td>用户id</td>
<td>用户名</td>
</tr>
<tr th:each="u : ${userList}">
<td th:text="${u.uid}">用户id</td>
<td th:text="${u.uname}">用户id</td>
</tr>
</table>
</body>
</html>

2、Freemarker模板
导入pop依赖
<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>

Freemarker去重
applicaton.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>我是freemaker</title>
</head>
<body>
我是freemaker
</body>
</html>
FreemakerController
package com.xyx.springboot1.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
/**
* @author xyx
* @site www.xyxmage.com
* @company xxx公司
* @create 2019-11-08 12:07
*/
@Controller
@RequestMapping("/freemarker")
public class FreemakerController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
mv.setViewName("list");//根据指定位置找到指定路径
return mv;
}
}

FreemakerController
package com.xyx.springboot1.controller;
import com.xyx.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;
/**
* @author xyx
* @site www.xyxmage.com
* @company xxx公司
* @create 2019-11-08 12:07
*/
@Controller
@RequestMapping("/freemarker")
public class FreemakerController {
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView mv = new ModelAndView();
mv.addObject("loginName","小花");
List list = new ArrayList();
list.add(new Role("1","普通用户"));
list.add(new Role("2","会员"));
list.add(new Role("3","超级会员"));
mv.addObject("roleList",list);
mv.setViewName("list");//根据指定位置找到指定路径
return mv;
}
}
获取值
!'未知’给他一个默认的值,在没有找到这个值的时候不报错
<body>
我是freemaker
<h2>获取值</h2>
${loginName!'未知'}

遍历
<h2>遍历</h2>
<table border="1" width="60%">
<tr>
<td>角色id</td>
<td>角色姓名</td>
</tr>
<#list roleList as role>
<tr>
<td>${role.rid}</td>
<td>${role.rname}</td>
</tr>
</#list>
</table>

获取头部

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
头部页面
</body>
</html>
<h2>包含页面</h2>
<#include 'common/head.ftl'/>
获取项目名
<h2>获取项目名</h2>
${springMacroRequestContext.contextPath}

如果只是一个斜杠是不会有的,

全局变量
global全局变量,xyx项目名
<#global xyx>
${springMacroRequestContext.contextPath}
</#global>
引用
<#include 'common/global.ftl'/>
${xyx}我是全局项目名

在这里我们设置了在role路径下所有如果需要转换到其他路径需要删除不然会进不去
# 设置ftl文件路径,默认是/templates,为演示效果添加role
template-loader-path: classpath:/templates/role
application.yml
server:
port: 80
servlet:
context-path: /xyx
spring:
thymeleaf:
cache: false

<div th:fragment="h1">
这是第一部分的内容
</div>
<div th:fragment="h2">
这是第二部分的内容
</div>
<div th:fragment="h3">
这是第三部分的内容
</div>
包含页面2
list.html
<h2>包含页面2</h2>
<div th:include="common/head2 :: h2"></div>

<h2>包含页面2</h2>
<div th:include="role/common/head2"></div>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Thymeleaf页面</title>
</head>
<body>
Thymeleaf页面
<h2>显示文本</h2>
<span th:text="${name}"></span>
<h2>显示html</h2>
<div th:utext="${msg}"></div>
<h2>循环</h2>
<table>
<tr>
<td>用户id</td>
<td>用户名</td>
</tr>
<tr th:each="u : ${userList}">
<td th:text="${u.uid}">用户id</td>
<td th:text="${u.uname}">用户id</td>
</tr>
</table>
<h2>包含页面2</h2>
<div th:include="role/common/head2 :: h2"></div>
</body>
</html>

本文深入讲解了SpringBoot中Thymeleaf和Freemarker两种模板引擎的使用方法,包括依赖配置、控制器编写、HTML页面展示及数据传递等关键步骤。
436

被折叠的 条评论
为什么被折叠?



