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)}${ ...
随机推荐
- 解决eclipse ctrl+鼠标左键不能用
选择[Window]菜单 Preferences ——>General——>Editors——>Text Editors——>Hyperlinking 把勾都点上,然后确定KE ...
- Java中常见数据结构:list与map -底层如何实现
1:集合 2 Collection(单列集合) 3 List(有序,可重复) 4 ArrayList 5 底层数据结构是数组,查询快,增删慢 6 线程不安全,效率高 7 Vector 8 底层数据结构 ...
- Javascript和Java获取各种form表单信息的简单实例
大家都知道我们在提交form的时候用了多种input表单.可是不是每一种input表单都是很简单的用Document.getElementById的方式就可以获取到的.有一些组合的form类似于che ...
- UML类图五种关系与代码的对应关系
转: UML类图中的五种关系的耦合强弱比较:依赖<关联<聚合<组合<继承 一.依赖关系: (一)说明 虚线+箭头 可描述为:Uses a 依赖是类的五种关系中耦合最小的一种关系 ...
- DOM - 5.事件冒泡 + 6.事件中的this
5.事件冒泡 如果元素A嵌套在元素B中,那么A被点击不仅A的onclick事件会被触发,B的onclick也会被触发.触发的顺序是"由内而外" .验证:在页面上添加一个table. ...
- [LeetCode] Longest Consecutive Sequence
Given an unsorted array of integers, find the length of the longest consecutive elements sequence. F ...
- Linux环境下stl库使用(map)
例子1: testMap.cpp #include <string.h> #include <iostream> #include <map> #include & ...
- 檢查RAC狀態
1.使用srvctl工具檢查RAC當前配置和狀態 $ srvctl config database -h Displays the configuration for the database. Us ...
- golang基础知识之文件操作
读取文件所有内容以及获得文件操作对象 package mainimport ( "bufio" "fmt" "io" "io/io ...
- SQL常见错误及处理方法
1.情况:数据库引擎安装失败,报类似权限不足的错误 解决:可能由于计算机名和用户名相同导致,更改计算机名,卸载干净重装即可