日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

JAVA入门[22]—thymeleaf

發布時間:2024/1/17 编程问答 38 豆豆
生活随笔 收集整理的這篇文章主要介紹了 JAVA入门[22]—thymeleaf 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一、thymeleaf官網

官網:https://www.thymeleaf.org/index.html

doc:https://www.thymeleaf.org/documentation.html

二、springmvc+thymeleaf從這里開始

1.修改pom.xml,引入相關依賴。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <dependencies> ????????<dependency> ????????????<groupId>junit</groupId> ????????????<artifactId>junit</artifactId> ????????????<version>3.8.1</version> ????????????<scope>test</scope> ????????</dependency> ????????<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf --> ????????<dependency> ????????????<groupId>org.thymeleaf</groupId> ????????????<artifactId>thymeleaf</artifactId> ????????????<version>3.0.2.RELEASE</version> ????????</dependency> ????????<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf-spring4 --> ????????<dependency> ????????????<groupId>org.thymeleaf</groupId> ????????????<artifactId>thymeleaf-spring4</artifactId> ????????????<version>2.1.2.RELEASE</version> ????????</dependency> </dependendies> 

2.xml方式配置thymeleaf視圖解析器:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 <!-- Thymeleaf View Resolver - implementation of Spring's ViewResolver?interface?--> ????<bean id="viewResolver"?class="org.thymeleaf.spring4.view.ThymeleafViewResolver"> ????????<property name="templateEngine"?ref="templateEngine"?/> ????????<property name="characterEncoding"?value="UTF-8"?/> ????</bean> ????<!-- Thymeleaf Template Engine (Spring4-specific version) --> ????<bean id="templateEngine"?class="org.thymeleaf.spring4.SpringTemplateEngine"> ????????<property name="templateResolvers"> ????????????<set> ????????????????<ref bean="templateResolver"?/> ????????????</set> ????????</property> ????</bean> ????<!-- Thymeleaf Template Resolver --> ????<bean id="templateResolver"?class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver"> ????????<property name="prefix"?value="/WEB-INF/templates/"?/> ????????<property name="templateMode"?value="HTML"?/> ????????<property name="suffix"?value=".html"></property> ????????<property name="characterEncoding"?value="UTF-8"></property> ????</bean>

3.在controller中為變量name賦值。

1 2 3 4 5 @RequestMapping(value="/index") public?String index(Model model){ ????model.addAttribute("name","world"); ????return?"index.html"; }

4.在index.html中使用thymeleaf語法讀取變量name的值。

1 2 3 4 5 6 7 8 9 <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"?xmlns:th="http://www.thymeleaf.org"> <head> ????<title>Title</title> </head> <body> <div>your name is:<span th:text="${name}"></span></div> </body> </html>

注意:需要修改html節點,添加xmlns:th="http://www.thymeleaf.org"

三、thymeleaf常見問題小結

1.如何添加鏈接:

1 <a th:href="@{/category/index}">首頁</a>
<a class="btn btn-xs btn-default" th:href="@{/role/edit(roleId=${r.roleId})}">編輯</a>

2.表單綁定示例:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 <form method="post"?th:object="${cate}"?th:action="@{/category/save}"?enctype="multipart/form-data"> ????????<table> ????????????<tr> ????????????????<td>id:</td> ????????????????<td><input type="text"?th:field="*{cateId}"></td> ????????????</tr> ????????????<tr> ????????????????<td>name:</td> ????????????????<td><input type="text"?th:field="*{cateName}"></td> ????????????</tr> ????????????<tr> ????????????????<td>file:</td> ????????????????<td> ????????????????????<input type="file"?accept="image/jpeg,image/png,image/jpg"?name="picture"> ????????????????</td> ????????????</tr> ????????????<tr> ????????????????<td colspan="2"> ????????????????????<input type="submit"?value="提交"> ????????????????</td> ????????????</tr> ????????</table> ????</form>

3.展示文本

1 <td th:text="${r.name}"></td>

如何替換子字符串

1 <span th:text="|welcome,${name}|"></span>

如何轉換日期格式

1 ${#dates.format(v.AddDate,'yyyy-MM-dd HH:mm:ss')}

4.如何在js讀取后臺數據

1 var url="[[${url}]]";

5.條件表達式

1 <td th:text="(${r.deleteFlag}==0)?'正常':'刪除'"></td>

6.thymeleaf如何實現switch選擇

1 2 3 4 5 <td th:switch="${r.type}"> ????<span th:case="page">頁面</span> ????<span th:case="module">模塊</span> ????<span th:case="*">其他</span> </td>

注意:默認值 用th:case="*"

7.th:object語法

首先在controller為對象賦值

1 2 3 4 5 6 7 8 9 10 11 12 @Controller @RequestMapping("/demo") public?class?DemoController { ????@RequestMapping("/index") ????public?String index(Model model){ ????????OrgResource resource=new?OrgResource(); ????????resource.setId("11"); ????????resource.setName("test"); ????????model.addAttribute("resource",resource); ????????return?"demo/index.html"; ????} }

使用*{}語法可以方便讀取th:object對象的屬性。

1 2 3 4 <div th:object="${resource}"> ????<div th:text="*{id}"></div> ????<div th:text="*{name}"></div> </div>

8.迭代 th:each

1 2 3 4 5 6 7 8 9 10 11 12 13 14 <th:block th:each="r,iterstat:${resources}"> ????<tr th:class="${iterstat.odd}?'odd'"> ????????<td th:text="${r.orderNo}"></td> ????????<td th:switch="${r.type}"> ????????????<span th:case="page">頁面</span> ????????????<span th:case="module">模塊</span> ????????</td> ????????<td th:text="(${r.deleteFlag}==0)?'正常':'刪除'"></td> ????????<td th:switch="${r.deleteFlag}"> ????????????<span th:case="0"><a>刪除</a></span> ????????????<span th:case="1"><a>恢復</a></span> ????????</td> ????</tr> </th:block>

9.如何使用Fragment layout布局

首先新建layout.html作為布局模板。

1 2 3 4 5 6 7 8 9 10 11 <!DOCTYPE html> <html lang="zh-CN"?xmlns:layout="http://www.thymeleaf.org"?xmlns:th="http://www.thymeleaf.org"> <head> ????<meta charset="UTF-8"> ????<title>Title</title> </head> …… <body> <div layout:fragment="content"></div> </body> </html>

然后在index.html中使用layout,并用頁面具體內容替代content fragment。

1 2 3 4 5 6 7 8 9 10 11 12 <!DOCTYPE html> <html layout:decorator="task/layout"?xmlns:layout="http://www.w3.org/1999/xhtml"> <head> ????<meta charset="UTF-8"> ????<title>Title</title> </head> <body> <div layout:fragment="content"> ??測試頁面 </div> </body> </html>

第一次使用layout布局的時候,調試了好半天就是不生效。后來找到了原因,dependency需要添加:

1 2 3 4 5 <dependency> ????<groupId>nz.net.ultraq.thymeleaf</groupId> ????<artifactId>thymeleaf-layout-dialect</artifactId> ????<version>${nz.net.ultraq.thymeleaflayout-version}</version> </dependency>

10.如何用if條件動態調整form action

1 <form th:action="@{/Video/{path}(path=${isCollect}?'CollectVideoList':'VideoList')}">

11.thymeleaf回顯富文本編輯器內容 將th:text換成th:utext即可。 <script type="text/plain" id="content" name="content" th:utext="${article.content}"></script>

?

?


? ? 本文轉自 陳敬(Cathy) 博客園博客,原文鏈接:http://www.cnblogs.com/janes/p/7234465.html,如需轉載請自行聯系原作者





總結

以上是生活随笔為你收集整理的JAVA入门[22]—thymeleaf的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。