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  
@ 昨日的世界
感谢支持
 
 
 
 
 
posted @ 2018-12-21 16:12  雷神约  阅读(1156)  评论(0编辑  收藏  举报