• springboot笔记-thymeleaf


    简介:
    Thymeleaf 是⾯向 Web 和独⽴环境的现代服务器端 Java 模板引擎,能够处理 HTML、XML、JavaScript、CSS 甚至纯文本。
    Thymeleaf 的作用域在 HTML 标签内,类似标签的一个属性来使用

    快速上手:

    添加pom依赖:
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

    在 application.properties 中添加配置:
    ##关闭 Thymeleaf 的缓存,否则在开发过程中修改页面不会立刻生效需要重启,生产可配置为 true
    spring.thymeleaf.cache=false

    编写一个简单的页面
    项目结构
    spring-boot-thymeleaf
    +-src
    +- main
    +- java
    +- resources
    +-static
    +-templates
    application.properties
    +- test
    +-pom.xml

    在templates目录下创建一个hello.html页面
    <!DOCTYPE html>
    <html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
    <meta charset="UTF-8"></meta>
    <title>Hello</title>
    </head>
    <body>
    <h1 th:text="${message}">Hello World</h1>
    </body>
    </html>

    ###所有使用 Thymeleaf 的页面必须在 HTML 标签声明 Thymeleaf:<html xmlns:th="http://www.thymeleaf.org">表明页面使用的是 Thymeleaf 语法。

    新建Controller
    @Controller
    public class HelloController {
    @RequestMapping("/")
    public String index(ModelMap map) {
    map.addAttribute("message", "http://www.baidu.com");
    return "hello";
    }
    }

    启动项目 在浏览器中输入网址:http://localhost:8080/,会出现下面的结果:
    http://www.baidu.com
    说明页面的值,已经成功的被后端传入的内容所替换。


    常用语法:

    赋值
    th:text="${message}"

    条件判断 If/Unless
    th:if="${flag == 'yes'}"
    th:unless="${flag != 'no'}"
    th:unless 与 th:if 恰好相反,只有表达式中的条件不成立,才会显示其内容。

    for 循环
    th:each="item,iterStat : ${items}"

    iterStat 称作状态变量,属性有:
    index,当前迭代对象的 index(从 0 开始计算);
    count,当前迭代对象的 index(从 1 开始计算);
    size,被迭代对象的大小;
    current,当前迭代变量;
    even/odd,布尔值,当前循环是否是偶数/奇数(从 0 开始计算);
    first,布尔值,当前循环是否是第一个;
    last,布尔值,当前循环是否是最后一个。

    URL
    Thymeleaf 对于 URL 的处理是通过语法@{...}来处理的
    <a th:href="@{http://www.baidu.com/{type}(type=${type})}">link1</a>

    也可以使用@{...}设置背景:
    <div th:style="'background:url(' + @{${img url}} + ');'">


    switchcase 选择
    switchcase 多用于多条件判断的场景下,举例:
    <div th:switch="${sex}">
    <p th:case="'woman'">她是一个姑娘...</p>
    <p th:case="'man'">这是一个爷们!</p>
    <!-- *: case的默认的选项 -->
    <p th:case="*">未知性别的一个家伙。</p>
    </div>


    内联 [ [ ] ]
    在 javascript 代码块里访问 model 中的数据,则要使用内联的方法
    内联文本:[ [...] ] 内联文本的表示方式,使用时,必须先用在 th:inline="text/javascript/none" 激活,th:inline 可以在父级标签内使用,甚至可以作为 body 的标签。

    如果想在脚本中使用后端传递的值,则必须使用脚本内联:
    th:inline="javascript"

    基本对象
    Thymeleaf 包含了一些基本对象,可以用于我们的视图中,这些基本对象使用 # 开头。
    #ctx:上下文对象
    #vars:上下文变量
    #locale:区域对象
    #request:(仅 Web 环境可用)HttpServletRequest 对象
    #response:(仅 Web 环境可用)HttpServletResponse 对象
    #session:(仅 Web 环境可用)HttpSession 对象
    #servletContext:(仅 Web 环境可用)ServletContext 对象

    内嵌变量
    dates:java.util.Date 的功能方法类
    calendars:类似 #dates,面向 java.util.Calendar
    numbers:格式化数字的功能方法类
    strings:字符串对象的功能类,contains、startWiths、prepending/appending 等
    objects:对 objects 的功能类操作
    bools:对布尔值求值的功能方法
    arrays:对数组的功能类方法
    lists:对 lists 的功能类方法
    sets:set 的实用方法
    maps:map 的实用方法

    接下来总结一下 Thymeleaf 表达式。
    表达式共分为以下五类:
    变量表达式:${...}
    选择或星号表达式:*{...}
    文字国际化表达式:#{...}
    URL 表达式:@{...}
    片段表达式:~{...}

    常用 th 标签

    页面常用的 HTML 标签几乎都有 Thymeleaf 对应的 th 标签。
    关键字 功能介绍 案例
    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="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少

    Thymeleaf 配置

    通过application.properties 文件灵活的配置 Thymeleaf 的各项特性,以下为 Thymeleaf 的配置和默认参数:

    # THYMELEAF (ThymeleafAutoConfiguration)
    #开启模板缓存(默认值:true)
    spring.thymeleaf.cache=true
    #检查模板是否存在,然后再呈现
    spring.thymeleaf.check-template=true
    #检查模板位置是否正确(默认值:true)
    spring.thymeleaf.check-template-location=true
    #Content-Type的值(默认值:text/html)
    spring.thymeleaf.content-type=text/html
    #开启MVC Thymeleaf视图解析(默认值:true)
    spring.thymeleaf.enabled=true
    #模板编码
    spring.thymeleaf.encoding=UTF-8
    #要被排除在解析之外的视图名称列表,用逗号分隔
    spring.thymeleaf.excluded-view-names=
    #要运用于模板之上的模板模式。另见StandardTemplate-ModeHandlers(默认值:HTML5)
    spring.thymeleaf.mode=HTML5
    #在构建URL时添加到视图名称前的前缀(默认值:classpath:/templates/)
    spring.thymeleaf.prefix=classpath:/templates/
    #在构建URL时添加到视图名称后的后缀(默认值:.html)
    spring.thymeleaf.suffix=.html
    #Thymeleaf 模板解析器在解析器链中的顺序,默认情况下,它排第一位,顺序从1开始,只有在定义了额外的 TemplateResolver Bean 时才需要设置这个属性。
    spring.thymeleaf.template-resolver-order=
    #可解析的视图名称列表,用逗号分隔
    spring.thymeleaf.view-names=

  • 相关阅读:
    解决DataGridView绑定List后不能排序的问题
    最新的皮肤下载
    我收录的名言
    HttpRequest访问Https带有证书并使用WSDL文档生成代理类方案(2)
    最新的Functions 类
    华兴软通短信接口简单使用WebServices版
    最新的SqlHelper 类
    闲来没事写个记事本玩玩!!!
    "基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系"证书验证失败的解决过程(3)
    FCK配置
  • 原文地址:https://www.cnblogs.com/qq99514925/p/11570025.html
Copyright © 2020-2023  润新知