Thymeleaf基础(二)
属性优先级
即执行顺序。
| Order | Feature | Attrbutes | 描述 |
|---|---|---|---|
| 1 | Fragment inclusion |
th:insertth:replace
|
包含 |
| 2 | Fragment iteration | th:each |
迭代 |
| 3 | Conditional evalution |
th:ifth:unlessth:switchth:case
|
条件判断 |
| 4 | Local variable definition |
th:objectth:with
|
本地变量 |
| 5 | Gerneral attribute modification |
th:attrth:attrprependth:attrappend
|
属性修改 |
| 6 | Specific attribute modification |
th:valueth:hrefth:src… |
特定属性修改 |
| 7 | Text (tag body modification) |
th:textth:utext
|
文本 |
| 8 | Fragment specification | th:fragment |
|
| 9 | Fragment removal | th:remove |
注释
标准HTML注释
<!--注释-->
Thymeleaf 解析器级注释块
删除
<!--/*-->和<!--*/-->之间的所有内容。
<!--/*-->
<div>
You can see me only before Thymeleaf processes me!
</div>
<!--*/-->
被注释的内容仅在静态页面中显示,当渲染时则被注释掉。
原型注释块
当模板静态打开时(比如原型设计),原型注释块所注释的代码将被注释,而在模板被执行时,这些被注释的代码就能被显示出来。
<span>hello</span>
<!--/*/
<div th:text="${...}">
...
</div>
/*/-->
<span>good bye!</span>
内联
内联表达式
[[...]]、[(...)]分别对应th:text与th:utext.
th:text与th:utext的区别。
传入数据为:
msg="This is <b>great!</b>"
原型与渲染后效果:
<p>The message is "[(${msg})]"</p>
=> <p>The message is "This is <b>great!</b>"</p>
<p>The message is "[[${msg}]]"</p>
=> <p>The message is "This is <b>great!</b>"</p>
禁用内联
有时要输出[[...]]等文本内容,须禁用内联表达式。
<p th:inline="none">
A double array looks like this:[[1,2,3],[4,5]]!
</p>
JavaScript内联
<script th:inline="javascript">
...
var username=/*[[${session.user.name}]]*/"Gertrud Kiwifruit";
...
</script>
/**/原型(即静态)时为被注释状态,渲染时为传入的数据。
css内联
传入数据为:
classname = 'main elems';
align = 'center';
原型与渲染后效果:
<style th:inline="css">
.[[${classname}]]{
text-align:[[${align}]];
}
</style>
=> <style th:inline="css">
.main\ elems{
text-align:center;
}
</style>
表达式基本对象
-
#ctx: 上下文对象。是org.thymeleaf.context.IContext或者org.thymeleaf.context.IWebContext的实现。 -
#local: 直接访问与java.util.Locale关联的当前的请求。
基本对象的一些使用:
request/session等属性
-
param: 用于检索请求参数。 -
session: 用于检索session属性。 -
application: 用于检索application/servlet上下文属性。
相关用法:
Web上下文对象
-
#request: 直接访问与当前请求关联的javax.servlet.http.HttpServletRequest对象。 -
#session: 直接访问与当前请求关联的javax.servlet.http.HttpServletSession对象。 -
#servletContext: 直接访问与当前请求关联的javax.servlet.ServletContext对象。
上下文对象的一些使用: