thymeleaf 基本表达式
如需了解thymeleaf以及thymeleaf整合spring,请参考《Thymeleaf模板引擎使用》、《Thymeleaf 集成spring》
${}
变量表达式(美元表达式,哈哈),用于访问容器上下文环境中的变量,功能同jstl中${}。
例如:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
...
//Create Servlet context
WebContext ctx = new WebContext(req, resp, this.getServletContext(), req.getLocale());
ctx.setVariable("helloword","hello thymeleaf,wellcome!");
//Executing template engine
templateEngine.process("home", ctx, resp.getWriter());
}
模板页面访问变量
<p><span th:text="${helloword}"></span></p>
*{}
选择表达式(星号表达式)。选择表达式与变量表达式有一个重要的区别:选择表达式计算的是选定的对象,而不是整个环境变量映射。也就是:只要是没有选择的对象,选择表达式与变量表达式的语法是完全一样的。那什么是选择的对象呢?是一个:th:object对象属性绑定的对象。
例如:
<div th: obj ect=" ${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:
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)}">
...do something...
</div>
其他工具对象表达式还有:
#dates #calendars #numbers #strings #objects #bools #arrays #lists #sets
更多详细表达式请访问:http://www.thymeleaf.org
thymeleaf 基本表达式的更多相关文章
- 初步认识thymeleaf:简单表达式和标签(二)
1.th:each:循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size, ...
- 初步认识thymeleaf:简单表达式和标签(一)
初步认识Thymeleaf:简单表达式和标签.(一) 本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,重新编辑的一篇文章,希望能对大家有所帮助.最后本文如果有哪 ...
- Thymeleaf 标准表达式语法
变量表达式${ } 在控制器中往页面传递几个变量: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 @Controller public class IndexController ...
- (二)Thymeleaf标准表达式之——简单表达式
2. 标准表达式(Standard Expression Syntax) 标准表达式包含以下几个方面: 简单表达式: 变量表达式: ${...} 选择变量表达式: *{...} 消息表达式: #{.. ...
- thymeleaf中th:attr用法以及相关的thymeleaf基本表达式
额,有人写的很好,我直接搬了 thymeleaf中th:attr用法 1.写死的单个属性值添加 th:attr="class=btn" 2.写死的多个属性值添加 th:attr=& ...
- Thymeleaf 页面表达式基础
转自:http://www.cnblogs.com/vinphy/p/4674247.html#undefined (一)Thymeleaf 是个什么? 简单说, Thymeleaf 是一个 ...
- Thymeleaf标准表达式
Thymeleaf的官网为: http://www.thymeleaf.org/ 一.变量表达式${-} 使用${-}括起来的表达式,称为变量表达式.该表达式的内容会显示在HTML标签体文本处. 该表 ...
- thymeleaf条件表达式
条件表达式形式:condition, then and else <tr th:class="${row.even}? 'even' : 'odd'"> ... < ...
- Thymeleaf模板表达式
日期格式.组件提取等. ${#dates.format(date)}${#dates.arrayFormat(datesArray)}${#dates.listFormat(datesList)}${ ...
随机推荐
- WinForm轻松实现自定义分页 (转载)
转载至http://xuzhihong1987.blog.163.com/blog/static/267315872011315114240140/ 以前都是做web开发,最近接触了下WinForm, ...
- osgconv 将多个模型合成一个模型
osgconv a.osg b.osg c.osg BigOne.ive 以上命令的作用是将a.osg.b.osg.c.osg三个模型合并到BigOne.ive模型
- device host global 函数要求
转自:https://kheresy.wordpress.com/2007/11/05/nvidia-cuda-api%EF%BC%88%E4%B8%8A%EF%BC%89/ Function typ ...
- python基础——返回函数
python基础——返回函数 函数作为返回值 高阶函数除了可以接受函数作为参数外,还可以把函数作为结果值返回. 我们来实现一个可变参数的求和.通常情况下,求和的函数是这样定义的: def calc_ ...
- Hadoop 2.5.2 eclipse plugin 编译 win7 集成
一.hadoop集群环境配置 参考我的前一篇文章(ubuntu + hadoop2.5.2分布式环境配置 http://www.cnblogs.com/huligong1234/p/4136331 ...
- 22.访问者模式(Vistor Pattern)
using System; using System.Collections; namespace ConsoleApplication5 { /// <summary> /// 访问者模 ...
- Notice: Only variable references should be returned by reference(PHP版本兼容性问题)
摘自:http://sushener.spaces.live.com/blog/cns!BB54050A5CFAFCDD!435.entry PHP5一个很让人恼火的一点就是BC(向后兼容)不是很理想 ...
- 重温WCF之WCF传输安全(十三)(2)基于SSL的WCF匿名客户端(转)
转载地址:http://www.cnblogs.com/lxblog/archive/2012/09/13/2683514.html 这一篇我们利用上一篇制作的证书,来演示一个基于SSL的WCF服务, ...
- .Learning.Python.Design.Patterns.2nd.Edition之单实例模式
可以慢慢理解.. 对照JAVA class Singleton(object): def __new__(cls): if not hasattr(cls, 'instance'): cls.inst ...
- Python无类再理解--metaclass,type
上次理解过一次,时间久了,就忘了.. 再学习一次.. http://blog.jobbole.com/21351/ ======================= 但是,Python中的类还远不止如此 ...