thymeleaf 常用属性以及基本表达式

常用属性

th:action

定义后台控制器路径,类似标签的action属性。

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

th:each

对象遍历,功能类似jstl中的标签。

th:field

常用于表单字段绑定。通常与th:object一起使用。 属性绑定、集合绑定。

<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:id

div id声明,类似html标签中的id属性。

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

th:href

定义超链接。value形式为@{/logout}

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

th:if

条件判断。

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

th:fragment

声明定义该属性的div为模板片段,常用与头文件、页尾文件的引入。常与th:include,th:replace一起使用。

<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一起使用进行表单数据绑定。

<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>
public class LoginBean implements Serializable{...}

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean) {...}

th:src

用于外部资源引入,类似于

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

th:text

文本显示。

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

th:value

用于标签复制,类似标签的value属性。

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

基本表达式

${}

变量表达式(美元表达式,哈哈),用于访问容器上下文环境中的变量,功能同jstl中${}。

<p><span th:text="${helloword}"></span></p>

*{}

选择表达式(星号表达式)。选择表达式与变量表达式有一个重要的区别:选择表达式计算的是选定的对象,而不是整个环境变量映射。也就是:只要是没有选择的对象,选择表达式与变量表达式的语法是完全一样的。那什么是选择的对象呢?是一个:th:object对象属性绑定的对象。

<div th: object=" ${session. user}" >
    <p>Name: <span th: text=" *{firstName}" >Sebastian</span>. </p>
    <p>Surname: <span th: text=" *{lastName}" >Pepper</span>. </p>
    <p>Nationality: <span th: text=" *{nationality}" >Saturn</span>. </p>
</div>

上例中,选择表达式选择的是th:object对象属性绑定的session. user对象中的属性。

#{}

消息表达式(井号表达式,资源表达式)。通常与th:text属性一起使用,指明声明了th:text的标签的文本是#{}中的key所对应的value,而标签内的文本将不会显示。

新建/WEB-INF/templates/home.html

<p th: text=" #{home. welcome}" >This text will not be show! </p>

新建/WEB-INF/templates/home.properties

home.welcome=this messages is from home.properties!

这里写图片描述
从测试结果可以看出,消息表达式通常用于显示页面静态文本,将静态文本维护在properties文件中也方面维护,做国际化等。

@{}

超链接url表达式。

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

maps

工具对象表达式。常用于日期、集合、数组对象的访问。这些工具对象就像是java对象,可以访问对应java对象的方法来进行各种操作。

<div th:if="${#maps.size(stuReqBean.students[__${rowStat.index}__].score) != 0}">
    <label>${score.key}:</label><input type="text" th:value="${score.value}"></input>
</div>
<div th:if="${#maps.isEmpty(stuReqBean.students[__${rowStat.index}__].score)}">
</div>
  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值