Thymeleaf 模板引擎用法


Thymeleaf 常用屬性

 

 

 

如需了解Thymeleaf 基本表達式,請參考《Thymeleaf 基本表達式》一文

 

th:action

定義后台控制器路徑,類似<form>標簽的action屬性。

例如:

<form id="login-form" th:action="@{/login}">...</form>

 

th:each

對象遍歷,功能類似jstl中的<c:forEach>標簽。

例如:

復制代碼
public class StudentRequestBean {

private List<Student> students;

...

}

public class Student implements Serializable{

private String firstName;

private String school;

...}

@RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(@ModelAttribute(value = "stuReqBean") StudentRequestBean stuReqBean,ModelMap model) {...}
復制代碼

 

復制代碼
<form id="login-form" th:action="@{/addStudent}" 

th:object="${stuReqBean}" method="POST">

<div class="student" th:each="stuIter,rowStat:${stuReqBean.students}">

<input type="text" class="firstName" value="" 

th:field="*{students[__${rowStat.index}__].firstName}"></input>

<input type="text" class="school" value="" 

th:field="*{students[__${rowStat.index}__].school}"></input>

...

</div>

</form>
復制代碼

 

上面的例子中通過選擇表達式*{}既能將表單綁定到后台的StudentRequestBean中的集合屬性students,也能將Servlet上下文中的StudentRequestBean中的List類型的students變量回顯,回顯時通過th:each進行遍歷。

注意1:綁定集合屬性元素下標的用法*{students[__${rowStat.index}__].firstName}

注意2:如果List<Student> students為null,頁面將無法顯示表單,后台必須給students初始化一個值,即:

List<Student > stus = new ArrayList<Student >();

stus .add(new Student ());

StudentRequestBean.setStudents(stus );

注意3:stuIter代表students的迭代器

 

th:field

常用於表單字段綁定。通常與th:object一起使用。 屬性綁定、集合綁定。

如:

復制代碼
public class LoginBean implements Serializable{...

private String username;

private List<User> user;

...}


public class User implements Serializable{...

private String username;;

...}


@RequestMapping(value = "/login", method = RequestMethod.POST)

public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {..}
復制代碼
復制代碼
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...

<input type="text" value="" th:field="*{username}"></input>

<input type="text" value="" th:field="*{user[0].username}"></input>

</form>
復制代碼

 

th:href

定義超鏈接,類似<a>標簽的href 屬性。value形式為@{/logout}

例如:

<a th:href="@{/logout}" class="signOut"></a>

 

th:id

div id聲明,類似html標簽中的id屬性。

例如:

<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>

 

th:if

條件判斷。

例如:

<div th:if="${rowStat.index} == 0">... do something ...</div>

 

th:include

見th:fragment

 

th:fragment

聲明定義該屬性的div為模板片段,常用與頭文件、頁尾文件的引入。常與th:include,th:replace一起使用。

例如:

聲明模板片段/WEBINF/templates/footer. html 

<div th: fragment=" copy" >

© 2011 The Good Thymes Virtual Grocery

</div>

引入模板片段

<div th: include=" /templates/footer : : copy" ></div>

<div th: replace=" /templates/footer : : copy" ></div>

 

th:object

用於表單數據對象綁定,將表單綁定到后台controller的一個JavaBean參數。常與th:field一起使用進行表單數據綁定。

例如:

public class LoginBean implements Serializable{...}


@RequestMapping(value = "/login", method = RequestMethod.POST)

public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}

 

<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>

 

th:src

用於外部資源引入,類似於<script>標簽的src屬性,常與@{}一起使用。

例如:

<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

 

th:replace

見th:fragment

 

th:text

文本顯示。

例如:

<td class="text" th:text="${username}" ></td>

 

th:value

用於標簽復制,類似<option>標簽的value屬性。

例如:

<option th:value="Adult">Adult</option>

<input  id="msg" type="hidden" th:value="${msg}" />




作者:ITPSC
出處:http://www.cnblogs.com/hjwublog/
溫馨提示:當您看到這篇文章時,我可能在很久之前就已經准備了,如果您覺得閱讀本文能讓你有所收獲,請點一下“推薦”按鈕或者“關注我”按鈕,您的肯定將是我寫作的動力!歡迎轉載,轉載請注明出處

 
分類:  thymeleaf
 
好文要頂  關注我  收藏該文   
9
3
 
 
 
« 上一篇: thymeleaf 基本表達式
» 下一篇: socket-詳細分析No buffer space available
posted @  2015-12-16 17:18 ITPSC 閱讀(85530) 評論(7) 編輯 收藏
 

 
#1樓   2018-10-23 09:56 | 測試5454  
我不知道你寫這些意義在哪里?
#2樓 [ 樓主] 2018-10-23 10:06 | ITPSC  
@ 天德社區
這位老師有何看法,交流交流?
#3樓   2018-10-24 16:17 | 測試5454  
@ ITPSC
不,我只是感覺你寫的太簡潔,會有很多人看不懂
#4樓   2018-10-24 16:19 | 測試5454  
@ ITPSC
引用@天德社區
這位老師有何看法,交流交流?

並沒有其他的意思
#5樓 [ 樓主] 2018-10-24 17:20 | ITPSC  
@ 天德社區
引用@ITPSC
不,我只是感覺你寫的太簡潔,會有很多人看不懂

結合前面幾篇文章,順着看應該能看懂吧,這篇常用屬性了解html的都應該能看懂。
#6樓   2018-11-13 15:33 | 昨日的世界  
我正好要用,看到這篇,當作入門手冊,很不錯!
#7樓 [ 樓主] 2018-11-13 15:42 | ITPSC  
@ 昨日的世界
感謝支持
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM