Thymeleaf相关补充
⒈理解Thymeleaf
- Java模板引擎。能夠處理HTML、XML、JavaScript、CSS甚至純文本。類似JSP、Freemarker
- 自然模板。原型即頁面
- 語法優雅易懂,OGNL、SpringEL
- 遵從Web標準。支持HTML5
⒉如何識別Thymeleaf標準方言
1.需要頁面引入命名空間
1 <html xmlns:th="http://www.thymeleaf.org"> 2 <head> 3 </head> 4 <body> 5 <span th:text="..."> 6 </body> 7 </html> 82.無需頁面引入命名空間
1 <span data-th-text="...">⒊語法
1.${...}:獲取變量值,OGNL
①獲取對象的屬性,調用方法
②使用內置的基本對象
③內置的一些工具對象
1 <span th:text="${book.author.name}">2.*{...}:變量選擇表達式,和${...}在功能上是一樣的,區別在于,它是在當前選擇的對象執行,而不是在整個上下文變量映射上。配合th:object使用簡化代碼
1 <div th:object="${book}"> 2 <span th:text="*{title}"></span> 3 </div>3.#{...}:獲取國際化內容
1 <span th:text="#{header.address.city}"></span>4.@{...}:定義URL
1 <!--鏈接表達式可以是相對的,在這種情況下,應用程序上下文將不會作為URL的前綴--> 2 <a th:href="@{../documents/report}"></a> 3 <!--也可以是服務器相對(同樣沒有應用程序上下文前綴)--> 4 <a th:href="@{~/contents/main}"></a> 5 <!--和協議相對(就像絕對URL,但瀏覽器將使用在顯示的頁面中使用的相同的HTTP或HTTPS協議)--> 6 <a th:href="@{//static.coreqi.cn/res/initial}"></a> 7 <!--當然,鏈接表達式也可以是絕對的--> 8 <a th:href="@{http://www.coreqi.cn}"></a>5.~{...}:片段引用表達式
1 <div th:fragment="copy"> 2 © 2019 <a href="http://www.coreqi.cn">Coreqi</a> 3 </div> 4 5 <div th:insert="~{footer :: copy}"></div>? 6.字面量(文字)
①文本
1 <span th:text="'Hello World!'"></span>②數字
1 <span th:text="2019"></span> 2 <span th:text="2019 + 2"></span>③布爾
1 <div th:if="${user.isAdmin()} == false"> 2 ... 3 </div>④Null
1 <div th:if="${user.phoneNum} == null"> 2 ... 3 </div>7.算術操作
①+、-、*、/、%
1 <div th:text="${users.count} % 2 == 0"> 2 .... 3 </div>8.比較和等價
①比較:>、<、>=、<=(gt、lt、ge、le)
1 <div th:if="${page.totalPages le 7}">②等價:==、!=(eq、ne)
1 <div th:if="${page.totalPages eq 7}">9.條件運算符
1 <div th:class="${row.even}? 'even' : 'odd'"></div>10.無操作(什么都不做)
1 <span th:text="${user.name}? : _">如果無操作則保留此處文本,不作覆蓋</span>11.設置屬性值
①設置任意屬性值 th:attr
1 <form th:attr="action=@{/coreqi}"> 2 <input type="submit" th:attr="value=#{subscribe.submit}"/> 3 </form>②設置指定屬性值
1 <form th:action="@{/coreqi}"> 2 <input type="submit" th:value="#{subscribe.submit}"/> 3 </form>③固定布爾屬性
1 <form th:action="@{/coreqi}"> 2 <input type="checkbox" name="option1" checked/> <!--HTML--> 3 <input type="checkbox" name="option2" checked="checked"/> <!--XHTML--> 4 <input type="checkbox" name="option3" th:checked="${user.active}"/> 5 </form>12.迭代器
①基本的迭代 th:each
1 <li th:each="book : ${books}" th:text="${book.title}"></li>狀態變量:用于跟蹤迭代器的狀態。
index【索引】、count【索引+1】、size【總數】、current【當前迭代的變量】、even/odd【是奇數還是偶數】、first【迭代器第一個】、last【迭代器最后一個】
1 <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'"></tr>? 13.條件語句
①th:if 判斷條件是否成立,true成立,false不成立,非0成立,null不成立。
1 <div th:if="${not #lists.isEmpty(prod.comments)}"> 2 ... 3 </div>②th:unless 不成立則執行
1 <div th:unless="${#lists.isEmpty(prod.comments)}"> 2 ... 3 </div>③switch 當有一個匹配后,剩下的全為false
1 <div th:switch="${user.role}"> 2 <p th:case="'admin'"></p> 3 <p th:case="#{roles.manager}"></p> 4 <p th:case="*"></p> 5 </div>?⒋Thymeleaf屬性的優先級
⒌注釋
①標準HTML/XML注釋
1 <!--<span th:text="'fanqi'"></span>-->②Thymeleaf解析器級注釋塊---在解析的過程中刪除掉<!--/*-->和<!--*/-->之間的所有內容
1 <!--/*--> 2 <div> 3 Hello World! 4 </div> 5 <!--*/-->③原型注釋塊
在靜態頁面的時候是注釋掉的,是不會顯示的。當被Thymeleaf解析器解析的時候,這些注釋的代碼將會顯示出來。
1 <span>Hello!</span> 2 <!--/*/ 3 <div th:text="${...}"> 4 ... 5 </div> 6 /*/--> 7 <span>Good Bye!</span>?⒍內聯
1.內聯表達式
①[[...]]或[(...)]分別對應于th:text【會對一些特殊符號進行轉義】和th:utext【不會對一些特殊符號進行轉義】
2.禁用內聯表達式 th:inline="none"
1 <p th:inline="none">輸出以下文本內容:[[1,2,3],[4,5]]!</p>3.JavaScript內聯
1 <script th:inline="javascript"> 2 var username=/*[[${session.user.name}]]*/ "Gertrud Kiwifruit"; 3 </script>4.css內聯
1 <style th:inline="css"> 2 .[[${classname}]]{ 3 text-align: [[${align}]]; 4 } 5 </style>⒎表達式基本對象
? 1.基本對象
①#ctx:上下文對象。是org.thymeleaf.context.IContext或者org.thymeleaf.context.IWebContext的實現。
1 ${#ctx.locale} 2 ${#ctx.variableNames} 3 ${#ctx.request} 4 ${#ctx.response} 5 ${#ctx.session} 6 ${#ctx.servletContext}②#locale:直接訪問與java.util.Locale關聯的當前的請求。
1 ${#locale}2.request/session等屬性對象
①param:用于檢索請求參數
1 ${param.foo} 2 ${param.size()} 3 ${param.isEmpty()} 4 ${param.containsKet('foo')} 5 ...②session:用于檢索session屬性
1 ${session.foo} 2 ${session.size()} 3 ${session.isEmpty()} 4 ${session.containsKey('foo')} 5 ...③application:用于檢索application/servlet上下文屬性
1 ${application.foo} 2 ${application.size()} 3 ${application.isEmpty()} 4 ${application.containsKey('foo')} 5 ...3.Web上下文對象
①#request:直接訪問與當前請求關聯的javax.servlet.http.HttpServletRequest對象。
1 ${#request.getAttribute('foo')} 2 ${#request.getParameter('foo')} 3 ${#request.getContextPath()} 4 ${#request.getRequestName()} 5 ...②#session:直接訪問與當前請求關聯的javax.servlet.http.HttpSession對象
1 ${#session.getAttribute('foo')} 2 ${#session.id} 3 ${#session.lastAccessedTime} 4 ...③servletContext:直接訪問與當前請求關聯的javax.servlet.ServletContext對象。
1 ${#servletContext.getAttribute('foo')} 2 ${#servletContext.contextPath} 3 ...?⒏相關配置
1 #Thymeleaf 編碼 2 spring.thymeleaf.encoding=UTF-8 3 #禁用Thymeleaf緩存,使用熱部署靜態文件 4 spring.thymeleaf.cache=false 5 #使用HTML5標準 6 spring.thymeleaf.mode=HTML5?
?
?
轉載于:https://www.cnblogs.com/fanqisoft/p/10529614.html
總結
以上是生活随笔為你收集整理的Thymeleaf相关补充的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有钱花提前还款后多久恢复额度
- 下一篇: CF1012B Chemical tab