thymeleaf 基本表达式

来源:互联网 发布:iphone视频编辑软件 编辑:程序博客网 时间:2024/06/10 00:50

${}

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

例如:

复制代码
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {...//Create Servlet contextWebContext ctx = new WebContext(req, resp, this.getServletContext(), req.getLocale());ctx.setVariable("helloword","hello thymeleaf,wellcome!");//Executing template enginetemplateEngine.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.propertieshome.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


原创粉丝点击