thymeleaf模板的使用——1,thymeleaf概述|| thymeleaf 的使用方法|| 如何修改Thymeleaf的默认存放地址||Thymeleaf的相关语法
thymeleaf模板的使用
1,thymeleaf概述
簡單說, Thymeleaf 是一個跟 Velocity、FreeMarker 類似的模板引擎,它可以完全替代 JSP 。相較與其他的模板引擎,它有如下三個極吸引人的特點:
1、Thymeleaf 在有網(wǎng)絡和無網(wǎng)絡的環(huán)境下皆可運行,即它可以讓美工在瀏覽器查看頁面的靜態(tài)效果,也可以讓程序員在服務器查看帶數(shù)據(jù)的動態(tài)頁面效果。這是由于它支持 html 原型,然后在 html 標簽里增加額外的屬性來達到模板+數(shù)據(jù)的展示方式。瀏覽器解釋 html 時會忽略未定義的標簽屬性,所以 thymeleaf 的模板可以靜態(tài)地運行;當有數(shù)據(jù)返回到頁面時,Thymeleaf 標簽會動態(tài)地替換掉靜態(tài)內(nèi)容,使頁面動態(tài)顯示。
2、Thymeleaf 開箱即用的特性。它提供標準和spring標準兩種方言,可以直接套用模板實現(xiàn)JSTL、 OGNL表達式效果,避免每天套模板、該jstl、改標簽的困擾。同時開發(fā)人員也可以擴展和創(chuàng)建自定義的方言。
3、Thymeleaf 提供spring標準方言和一個與 SpringMVC 完美集成的可選模塊,可以快速的實現(xiàn)表單綁定、屬性編輯器、國際化等功能。
Spring Boot項目Thymeleaf模板頁面存放位置
查看ThymeleafAutoConfiguration
查看ThymeleafProperties
thymeleaf前后綴的配置 默認前綴為? classpath:/templates/? ?后綴是? ?.html
4,通過Controller跳轉到Thymeleaf的頁面?
http://localhost:8080/index/hello
訪問??http://localhost:8080/index/userManager
?如何修改Thymeleaf的默認存放地址
Thymeleaf的相關語法
1,簡單表達式 ??
1、變量的表達式:${...} ????取作域里面的值 ??Model
2、選擇變量表達式:*{...}
3、信息表達式:#{...} ????#取IOC容器里面的值 ?
4、鏈接URL表達式:@{...} ? <a href="user/query.action"> ? ? ? <a th:href="@{user/query.action}"??Href? ?Actio? ?Src
2,字面值 ?th:text
1、文本文字:'one text', 'Another one!',…
2、文字數(shù)量:0, 34, 3.0, 12.3,…
3、布爾型常量:true, false
4、空的文字:null
5、文字標記:one, sometext, main,…
3,文本處理
1、字符串并置:+
2、文字替換:|The name is ${name}|
4,表達式基本對象
1、#ctx:上下文對象
2、#vars:上下文變量
3、#locale:上下文語言環(huán)境
4、#httpServletRequest:(只有在Web上下文)HttpServletRequest對象 ??
5、#httpSession:(只有在Web上下文)HttpSession對象。 ? ? ? ? ? ? ?
用法:<span th:text="${#locale.country}">CN</span>.
5,實用工具對象
#dates: java.util的實用方法。對象:日期格式、組件提取等.
#calendars:類似于#日期,但對于java.util。日歷對象
#numbers:格式化數(shù)字對象的實用方法。
#strings:字符串對象的實用方法:包含startsWith,將/附加等。
#objects:實用方法的對象。
#bools:布爾評價的實用方法。
#arrays:數(shù)組的實用方法。
#lists:list集合。
#sets:set集合。
#maps:map集合。
#aggregates:實用程序方法用于創(chuàng)建聚集在數(shù)組或集合.
#messages:實用程序方法獲取外部信息內(nèi)部變量表達式,以同樣的方式,因為它們將獲得使用# {…}語法
#ids:實用程序方法來處理可能重復的id屬性(例如,由于迭代)。
?
Thymeleaf讀取xxx.properties里面的對象【了解】國際化
修改application.properties
創(chuàng)建showStudent.html
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title><link rel="stylesheet" href="/layui/css/layui.css" media="all"> </head> <body><fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;"><legend>學生信息</legend> </fieldset> <div style="padding: 20px; background-color: #F2F2F2;"><div class="layui-row layui-col-space15"><div class="layui-col-md6"><div class="layui-card"><div class="layui-card-body"><span>學生編號:</span><span th:text="#{student.id}"></span><br><span>學生姓名:</span><span th:text="#{student.name}"></span><br><span>學生年齡:</span><span th:text="#{student.age}"></span><br><span>學生性別:</span><span th:text="#{student.sex}"></span><br><span>學生生日:</span><span th:text="#{student.birth}"></span><br><span>學生電話:</span><span th:text="#{student.phone}"></span><br></div></div></div></div> </div><!--<div th:text="#{welcome('小明','WHSXT')}">--></div><script src="/layui/layui.js" charset="utf-8"></script> </body> </html>出現(xiàn)的問題
解決辦法創(chuàng)建I18NConfig
總結
以上是生活随笔為你收集整理的thymeleaf模板的使用——1,thymeleaf概述|| thymeleaf 的使用方法|| 如何修改Thymeleaf的默认存放地址||Thymeleaf的相关语法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AOP开发——在不修改源代码的前提下,对
- 下一篇: Thymeleaf读取model里面的对