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
1、传参
th:href="@{/console/createBilling(appKey=${appInfoForm.appKey})}"
2、if判断,并连接用and
th:if="${appInfoForm.billingId ne 0 and appInfoForm.billingPayed eq false}"
3、日期的格式化
th:text="${#dates.format(billingForm.startTime,'yyyy-MM-dd HH:mm:ss')}"
4、单击事件
th:οnclick="'javascript:deleteBilling(\''+${appInfoForm.billingId}+'\',\''+${appInfoForm.appKey}+'\');'"
其中deleteBilling是在js中定义好的方法。
5、html页面中的js必须用/*<![CDATA[*/ this is JavaScript code /*]]>*/包裹,否则会出现些许问题,比如js中&符号的出现会导致页面运行出错。
<script type="text/javascript">
/*<![CDATA[*/
function themPage(value,type){
alert(value);
}
function themPage2(value,type){
alert(type);
}
/*]]>*/
</script>
6、循环select 的option项
<select class="form-control" id="slaId" th:field="*{slaId}">
<option value="0">请选择</option>
<option th:each="slaItem : ${slaItems}" th:value="${slaItem.id}" th:text="${slaItem.name}">Options</option>
</select>
7、按原样输出带有html标签的字符
使用th:utext就可以,假设msg是java后端传过来的内容是"<br/>"的session变量,那么下面的语句中将会在span标签里面进行一次换行:
<span th:utext="${session.msg}"></span>
8、赋值判断
<li>支持自定义知识库:<strong th:text="${slaItem.supportCustomize eq 1}?'是':'否'"></strong></li>
9、判断集合长度是否是0
th:if="${#lists.size(appInfoFormList)} eq 0"
原文:https://blog.csdn.net/zhengxiangwen/article/details/64906066
作者:ITPSC
出处:http://www.cnblogs.com/hjwublog/