一、thymeleaf
THymeleaf是在有网络和无网络的环境下都可以运行,也就是说可以让美工在浏览页面查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是因为它支持HTML原型,然后在HTML标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。
Thymeleaf开箱即用的特性。它提供保准个spring标准的两种方言,可以直接套用模板实现jstl,ognl表达式效果,避免每天套模板、改jstl、改标签的困扰。
THymeleaf是一个HTML页面。
使用thymeleaf需要添加依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
代码实例
<!DOCTYPE html>
<html lang="en">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>thymeleaf模板 用户列表</title>
</head>
<body>
<!--th:text 用于展示 无论${}为null还是“” 都显示空白字符-->
<h1 th:text="${title}"></h1>
<!--当title1没有值时,未知才会充当默认值出现-->
<h1 th:text="${title1}">未知</h1>
<h1>循环</h1>
<!--th:each="变量 : $数据源" 用于循环-->
<table border="1" width="500px">
<thead>
<tr>
<td>用户id</td>
<td>用户名</td>
<td>用户描述</td>
</tr>
</thead>
<tbody>
<tr th:each="list : ${lists}">
<td th:text="${list.uid}"></td>
<td th:text="${list.userName}"></td>
<td th:text="${list.desc}"></td>
</tr>
</tbody>
</table>
<h1>th:value</h1>
<select>
<option th:each="list :${lists}" th:text="${list.userName}" th:value="${list.uid}"></option>
</select>
</body>
</html>
二、freemaker
Freemaker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
建好项目后要配置freemaker
OK之后可以看到freemaker标志,后缀是ftl
代码实例
yml配置
spring:
thymeleaf:
cache: false
freemarker:
# \u8BBE\u7F6E\u6A21\u677F\u540E\u7F00\u540D
suffix: .ftl
# \u8BBE\u7F6E\u6587\u6863\u7C7B\u578B
content-type: text/html
# \u8BBE\u7F6E\u9875\u9762\u7F16\u7801\u683C\u5F0F
charset: UTF-8
# \u8BBE\u7F6E\u9875\u9762\u7F13\u5B58
cache: false
# \u8BBE\u7F6Eftl\u6587\u4EF6\u8DEF\u5F84\uFF0C\u9ED8\u8BA4\u662F/templates\uFF0C\u4E3A\u6F14\u793A\u6548\u679C\u6DFB\u52A0role
template-loader-path: classpath:/templates
mvc:
static-path-pattern: /static/**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>freemaker模板</title>
<!--这里是包含了一个js,提供方便-->
<#include 'common.ftl'>
</head>
<body>
<h2>取值</h2>
welcome 【${name}】 to freemarker!
<h3>提供默认值</h3>
<#--当name1为null时,运行页面时会报错,只要加个感叹号!就OK了。感叹号后面的值为默认值-->
welcome 【${name1!'默认值'}】 to freemarker!
<h1>非空判断</h1>
<#--判断name是否存在-->
<#if name?exists><#--或者name??-->
王源
</#if>
<h1>条件</h1>
<#if sex=='boy'>
boy
<#elseif sex=="girl">
girl
<#else>
小思思
</#if>
<h1>循环</h1>
<table border="1" width="500px">
<thead>
<tr>
<td>角色id</td>
<td>角色名</td>
<td>角色描述</td>
</tr>
</thead>
<tbody>
<#list roles as role>
<tr>
<td>${role.rid}</td>
<td>${role.rolename}</td>
<td>${role.desc}</td>
</tr>
</#list>
</tbody>
</table>
<h1>获取项目名(设置局部变量assign和全局变量global)</h1>
<#--将项目名复制给ctxl这个变量 ,这个变量的作用域在当前页面-->
<#assign ctxl>
${springMacroRequestContext.contextPath}
</#assign>
<#--将项目名复制给ctx2这个变量 ,这个变量的作用域在整个页面-->
<#global ctxl2>
${springMacroRequestContext.contextPath}
</#global>
${ctxl},${ctxl2}
<h1>include 包含</h1>
<#include 'foot.ftl'>
</body>
</html>
common.ftl
<#assign ctx>
${springMacroRequestContext.contextPath}
</#assign>
<base href="${ctx}">
<script type="text/javascript" href="static/js/layui.js"></script>