thymeleaf 基本表达式

it2022-05-05  157

文章主目录

${}*{}#{}@{}#maps

 在JS中引用后台传过来的变量,需要这样表达

var avatar=[[${ytj1004.avatar}]];

直接这样写var avatar=${ytj1004.avatar};在IDEA会报错。

 

 如需了解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

转载自:https://www.cnblogs.com/hjwublog/p/5051632.html 感谢作者ITPSC

 


最新回复(0)