SpringBoot(六)springboot整合themleaf

Themleaf

    thymeleaf 是新一代的模板引擎,在spring4.0中推荐使用thymeleaf来做前端模版引擎。

  • Thymeleaf 在有网络和无网络的环境下皆可运行,即它可以让美工在浏览器查看页面的静态效果,也可以让程序员在服务器查看带数据的动态页面效果。这是由于它支持 html 原型,然后在 html 标签里增加额外的属性来达到模板+数据的展示方式。浏览器解释 html 时会忽略未定义的标签属性,所以 thymeleaf 的模板可以静态地运行;当有数据返回到页面时,Thymeleaf 标签会动态地替换掉静态内容,使页面动态显示。

  • Thymeleaf 开箱即用的特性。它提供标准和spring标准两种方言,可以直接套用模板实现JSTL、 OGNL表达式效果,避免每天套模板、该jstl、改标签的困扰。同时开发人员也可以扩展和创建自定义的方言。

  • Thymeleaf 提供spring标准方言和一个与 SpringMVC 完美集成的可选模块,可以快速的实现表单绑定、属性编辑器、国际化等功能。

 springboot整合themleaf

   1.  项目依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

   2.  application.properties配置

#thymeleaf
#默认到resource/templates目录下寻找
spring.thymeleaf.suffix=.html
spring.thymeleaf.check-template-location=true
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
spring.thymeleaf.cache=false
spring.thymeleaf.mode=LEGACYHTML5

    3. 工程结构

SpringBoot(六)springboot整合themleaf

 HelloController


import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

/*
 * @author uv
 * @date 2018/9/25 18:06
 *
 */
@Controller
public class HelloController {

    @RequestMapping("hello")
    public String hello(String name, Model model) {
        List<String> list = new ArrayList<>();
        list.add("a");
        list.add("b");
        list.add("c");
        list.add("d");
        model.addAttribute("name", name);
        model.addAttribute("strList", list);
        model.addAttribute("show", false);
        return "hello";
    }
}

   Hello.html

  引用命名空间 <html xmlns:th="http://www.thymeleaf.org"> 

<html xmlns:th="http://www.thymeleaf.org">
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
<!--拼接字符串使用双|包括-->
<h1 th:text="|Hello ${name}|"></h1>
<!--遍历->
<ul th:each="str:${strList}">
  <li th:text="${str}"></li>
</ul>
<!--判断-->
<div th:if="${show}">可见</div>
</body>
</html>

   4.  运行结果

SpringBoot(六)springboot整合themleaf

   5.  常用标签

关键字         功能介绍 案例
th:id          替换id  <input th:id="'xxx' + ${collect.id}"/> 
th:text 文本替换 <p th:text="${collect.description}">description</p>
th:utext 支持html的文本替换 <p th:utext="${htmlcontent}">conten</p>
th:object 替换对象 <div th:object="${session.user}"> 
th:value 属性赋值 <input th:value="${user.name}" /> 
th:with 变量赋值运算 <div th:with="isEven=${prodStat.count}%2==0"></div> 
th:style 设置样式 th:style="'display:' + @{(${sitrue} ? 'none' : 'inline-block')} + ''" 
th:onclick 点击事件 th:onclick="'getCollect()'" 
th:each 属性赋值 tr th:each="user,userStat:${users}"> 
th:if 判断条件  <a th:if="${userId == collect.userId}" > 
th:unless 和th:if判断相反 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:href 链接地址 <a th:href="@{/login}" th:unless=${session.user != null}>Login</a> /> 
th:switch 多路选择 配合th:case 使用 <div th:switch="${user.role}"> 
th:case th:switch的一个分支  <p th:case="'admin'">User is an administrator</p>
th:fragment 布局标签,定义一个代码片段,方便其它地方引用 <div th:fragment="alert">
th:include 布局标签,替换内容到引入的文件 <head th:include="layout :: htmlhead" th:with="title='xx'"></head> /> 
th:replace 布局标签,替换整个标签到引入的文件 <div th:replace="fragments/header :: title"></div> 
th:selected selected选择框 选中 th:selected="(${xxx.id} == ${configObj.dd})"
th:src 图片类地址引入 <img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" /> 
th:inline 定义js脚本可以使用变量 <script type="text/javascript" th:inline="javascript">
th:action 表单提交的地址 <form action="subscribe.html" th:action="@{/subscribe}">
th:remove 删除某个属性 <tr th:remove="all"> 1.all:删除包含标签和所有的孩子。2.body:不包含标记删除,但删除其所有的孩子。3.tag:包含标记的删除,但不删除它的孩子。4.all-but-first:删除所有包含标签的孩子,除了第一个。5.none:什么也不做。这个值是有用的动态评估。
th:attr 设置标签属性,多个属性可以用逗号分隔 比如 th:attr="[email protected]{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。

更多详细用法请参考官网:https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#introducing-thymeleaf