using thymeleaf之二简单表达式

本文深入探讨Thymeleaf标准方言中的简单表达式,包括变量表达式的语法和用途,如何使用${ }获取上下文映射的变量,与OGNL表达式进行比较。
摘要由CSDN通过智能技术生成


2 thymeleaf标准方言

2.1简单表达式

2.1.1 变量表达式

语法格式为${ }.与OGNL表达式类似,都是取得context map的变量.

<ol>
		<li>${title}:<span th:text = "${title}">title</span></li>
		<li>${session.sessionStr}:<span th:text = "${session.sessionStr}">sessionStr1</span></li>
		<li>${#httpSession.getAttribute('sessionStr')}:<span th:text = "${#httpSession.getAttribute('sessionStr')}">sessionStr2</span></li>
		<li>${requestStr}:<span th:text = "${requestStr}">requestStr1</span></li>
		<li>${#httpServletRequest.getAttribute('requestStr')}:<span th:text = "${#httpServletRequest.getAttribute('requestStr')}">requestStr2</span></li>
		<li>${#vars[title]}:<span th:text = "${#vars[title]}">title</span></li>
		<li>${#vars.get('title')}:<span th:text = "${#vars.get('title')}">title</span></li>
		<li>${#vars.get('requestStr')}:<span th:text = "${#vars.get('requestStr')}">title</span></li>
		<li>${#vars.size()}:<span th:text = "${#vars.size()}">size</span></li>
		<li>${#ctx.variables[title]}:<span th:text = "${#ctx.variables[title]}">title</span></li>
		<li>${#ctx.variables.get('title'):<span th:text = "${#ctx.variables.get('title')}">title</span></li>
		<li>${#ctx.locale.country}:Established locale country: <span th:text="${#ctx.locale.country}">US</span>.</li>
		<li>${#ctx.httpSession.getAttribute('sessionStr')}:<span th:text = "${#ctx.httpSession.getAttribute('sessionStr')}">title</span></li>
		<li>${#ctx.httpServletRequest.getAttribute('requestStr')}:<span th:text = "${#ctx.httpServletRequest.getAttribute('requestStr')}">title</span></li>
		<li>${#lists.size(testList)}:<span th:text = "${#lists.size(testList)}">2</span></li>
		<li>${#lists.size(testList)}+2:<span th:text = "${#lists.size(testList)}+2">2</span></li>
		<li>${#lists.size(testList)}+'prepend':<span th:text = "${#lists.size(testList)}+'prepend'">2+'prepend'</span></li>
		<li>${#calendars.format(today,'dd MMMM yyyy')}:今天是<span th:text = "${#calendars.format(today,'dd MMMM yyyy')}">13 May 2011</span></li>
		<li>${#calendars.format(today,'dd MMMM yyyy')}:今天是<span th:text = "${#calendars.format(today,'yyyy-MM-dd')}">13 May 2011</span></li>
		<li>${#locale.country}:Established locale country: <span th:text="${#locale.country}">US</span>.</li>
		
</ol>


2.1.2选择表达式,又名星号表达式
语法格式为*{ }.一般与th:object配合使用,用于获取对象的属性值

2.1.3链接表达式
语法格式为@{ }.用于hr ef属性的值
共有五种类型:
(1)绝对URL(Absolute URLs)

通过指定协议名称(包含http://或https://开头)
e.g.
<a th:href="@{http://www.thymeleaf/documentation.html}"></a>
equals:
<a href="http://www.thymeleaf/documentation.html"></a>
(2)上下文相关的URL(Context-relative URLs)
最常用的如应用程序myapp的访问地址:http://localhost:8080/myapp,那么myapp就是上下文名称。
e.g.<a th:href="@{/order/list}"></a>
equals:<a href="/myapp/order/list"></a>
说明:“/”表示从应用程序根路径开始访问
去掉“/”表示相对路径
(3)相对于服务器的URL(Server-relative URLs)
用法类似2,连接到同一服务器不同应用程序中
e.g.<a th:href="@{~/billing-app/showDetails.htm}"></a>
equals:
<a href="/billing-app/showDetails.htm"></a>
note:当前应用程序的上下文myapp将被忽略
(4)协议相对URL(Protocol-relative URLs)
通常用于,包括外部资源,如样式,脚本等。保证协议(HTTP,HTTPS)用于显示当前页面的绝对URL。
e.g.<script th:src="@{//scriptserver.example.net/myscript.js}">...</script>
equals:<script src="//scriptserver.example.net/myscript.js">...</script>
(5)带参数的URL表达式
表达式中可以携带一个或多个参数.e.g.
1.<a th:href="@{/order/details(id=3)}"></a>
equals:<a href="/order/details?id=3"></a>
2.携带多参数:<a th:href="@{/order/details(id=3,action='show_all')}"></a>
equals: <a href="/order/details?id=3&action=show_all"></a>
2.1.4 消息表达式
支持国际化。(待续)


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值