網站首頁 編程語言 正文
模板引擎:
模板引擎是為了讓用戶顯示界面和業務數據內容分離而產生的,可以生成特定形式的文檔,常用的格式有HTML、XML以及其他格式的文檔。
?常見的模板引擎:
jsp、freemarker、velocity、themeleaf
(1)jsp? ?優點:1>>功能強大,可以寫java代碼,因此jsp代碼可以跨平臺。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2>>支持jsp標簽、支持表達式語言
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3>>性能良好,jsp文件會編譯成class文件執行,有很好的性能表現
? ? ? ? ? ? ? ? ? ? ? ? ? ? ?4>>官方標準,用戶群廣,豐富的第三方jsp標簽庫
? ? ? ? ? ? ? ? ? 缺點:調試難度高,因為是編譯成class文件的,所以在報錯時是class文件報錯不是jsp。已經很少有公司使用了
(2)freemarker? ?優點:1>>不能編寫java代碼,可以實現嚴格的MVC分離。
? ? ? ? ? ? ? ? ? ? ? ? ? ????????? ? ? 2>>對jsp標簽支持良好
? ? ? ? ? ? ? ? ? ? ? ? ????????? ? ? ? 3>>內置大量的常用功能、宏定義(類似于jsp標簽),使用非常簡單
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? ?4>>使用表達式語言
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 缺點:不是官方標準,第三方標簽庫不如jsp多
(3)velocity? ?優點:1>>不能編寫java代碼,可以實現嚴格的MVC分離。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2>>其官方好像表示比jsp功能還要好一些
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3>>使用表達式語言,jsp的表達式語言可能就是學velocity的
? ? ? ? ? ? ? ? ? ? ? ? 缺點:1>>不是官方標準,第三方標簽庫不如jsp多
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?2>>對jsp標簽支持不夠好
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?3>>已經很久沒有維護了?
(4)thymeleaf:Thymeleaf是用來開發Web和獨立環境項目的服務器端的Java模版引擎。
????????Spring官方支持的服務的渲染模板中,并不包含jsp。而是Thymeleaf和 Freemarker等,而Thymeleaf與SpringMVC的視圖技術,及SpringBoot的 自動化配置集成非常完美,幾乎沒有任何成本,你只用關注Thymeleaf的語法即可。
優點? ?1>>動靜結合:Thymeleaf 在有無網絡上都可以運行,即可以瀏覽靜態頁面(忽略未定義的標簽屬性)也可以瀏覽動態頁面(因為其支持HTML原型,利用標簽的額外屬性來達到模板+數據的展示)。
? ? ? ? ? ?2>>開箱即用:它提供標準和spring標準兩種方言,可以直接套用模板實現JSTL、OGNL表達式效果,避免每天套模板、改jstl、改標簽的困擾。
? ? ? ? ? ?3>>多方言支持:thymeleaf提供了spring標準方言和一個與SpringMVC完美集成的可選模塊,快速實現表單綁定、屬性編輯器、國際化等功能。
? ? ? ? ? ?4>>與SpringBoot完美整合:SpringBoot提供了Thymeleaf的默認配置,并且為Thymeleaf設置了視圖解析器,我們可以像以前操作jsp一樣來操作Thymeleaf。代碼幾乎沒有任何區別,就是在模板語法上有區別
缺點? ?:模板必須符合xml規范
thymeleaf的常用指令介紹:
(定義學生對象《student》屬性《sname、ssex、birthday》)
首先在HTML中聲明:<html xmlns:th="http://www.thymeleaf.org" >
獲取值的ognl表達式:${(需要引入的數據)}
指令是:th:xxxxx??用兩個中括號也可以直接${}內容,方便替換
<!-- 用兩個中括號也可以直接${}內容,方便替換 -->
<div><span style="color: aqua ">我是張三的妹妹</span>[[${name}]]</div>
<script type="text/javascript">
alert("[[${name}]]");
</script>
雙標簽↓:
<!-- 給雙標簽中間賦值 th:text 不僅僅是賦值的html轉換 th:utext -->
<h1 th:text="${數據}"> 結果呈現</h1>
<!-- 如果servlet傳遞的是一個對象(student),可以直接點起屬性(sname,ssex,birthday) -->
<h1 th:text="${student.sname}"> 結果呈現</h1>
<!-- 時間的轉換格式 -->
<p th:text="${#dates.format(stu.birthday,'yyyy-MM-dd')}"></p>
<!--不僅僅是賦值的html轉換 th:utext -->
<!--session.setAttribute("err", new MsgUtil(MsgUtil.bgcolor.danger(枚舉值), "賬號或密碼錯誤").getMshtml()(HTML)); -->
<div id="result" th:utext="${session.err}"></div>
單標簽↓:
<input type="number" th:value="*{classid}"/>
session容器中拿取數據的兩種方式↓:
<!--方式一:語法糖 -->
<div th:text="${session.stu2.sname}"></div>
<!--方式二:不使用語法糖 -->
<div th:text="${#session.getAttribute('stu2')['sname']}"></div>
三元運算符 兩種方式↓:
?
<div th:text="${sex} == 1 ? '男' : '女'"></div>
<div th:text="${name1} ?:此人沒有名字"></div>
th:if 當條件為真時渲染頁面? ???th:unless 當條件為假時渲染頁面
<!-- if是為true顯示 unless是為false-->
<div th:if="${sex} == 1">
<input type="radio" name="sex" value="男" checked/>男
<input type="radio" name="sex" value="女"/>女
</div>
字符串拼接
<!-- 字符串的拼接 -->
<div th:text="'我是張三的妹妹' +${name}"></div>
<div th:text="|我是張三的妹妹${name}|"></div>
遍歷
<h1>學生信息表</h1>
<table border="2" align="center">
<tr >
<th>序號</th>
<th>編號</th>
<th>姓名</th>
<th>性別</th>
<th>生日</th>
<th>班級編號</th>
<th>下標</th>
<th>個數</th>
<th>對象地址值</th>
<th>是否是偶數行</th>
<th>是否是奇數行</th>
<th>是否是首行</th>
<th>是否是尾行</th>
</tr>
<tr th:each="s,x : ${slist}" th:classappend="${x.odd} ? 'red': 'blue' ">
<td th:text="${x.count}"></td>
<td th:text="${s.sid}"></td>
<td th:text="${s.sname}"></td>
<td th:text="${s.ssex}"></td>
<td th:text="${#dates.format(s.birthday,'yyyy-MM-dd')}"></td>
<td th:text="${s.classid}"></td>
<td th:text="${x.index}"></td>
<td th:text="${x.size}"></td>
<td th:text="${x.current}"></td>
<td th:text="${x.even}"></td>
<td th:text="${x.odd}"></td>
<td th:text="${x.first}"></td>
<td th:text="${x.last}"></td>
</tr>
</table>
原文鏈接:https://blog.csdn.net/l1050188952/article/details/125671569
相關推薦
- 2022-04-23 一起來了解python的基本輸入和輸出_python
- 2022-07-11 python利用多線程+隊列技術爬取中介網互聯網網站排行榜_python
- 2022-07-08 Pytest如何使用mark的方法_python
- 2023-07-02 Python3中zip()函數知識點小結_python
- 2023-10-24 解決Ubuntu下載速度或更新速度緩慢問題
- 2021-12-22 Linux一次性計劃任務at命令使用詳解_Linux
- 2023-03-20 C#字符集編碼的使用及說明_C#教程
- 2022-09-25 自動微分----pytorch中的梯度運算與反向傳播函數(預備知識)
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支