Thymeleaf使用说明

Thymeleaf使用说明

javascript操作:

a.<script type="text/javascript" th:inline="javascript">    --标签

b.var page_url =/*[[@{/shop/page}]]*/;      --路径

c.var value =/*[[${entity.id}]]*/;     --获取值
1
2
3
4
5
1
2
3
4
5
select操作:

a.遍历数据:
        <select id="categorys" class="form-control">
            <option th:each="c,userStat:${categorys}" th:value="${c.id}" th:text="${c.name}"></option>
        </select>
b.表单回显:
        <select th:field="*{entity.courseType}" name="courseType" class="form-control">
             <option th:each="cts,userStat:${courseType}" th:value="${cts.id}" th:text="${cts.name}"></option>
        </select>
1
2
3
4
5
6
7
8
1
2
3
4
5
6
7
8
radio操作:

    <label style=" 80px;"> 
        <input th:field="*{entity.mode}" type="radio" name="mode" value="0"/>  重量
    </label>
    <label style=" 80px;">
        <input th:field="*{entity.mode}" type="radio" name="mode" value="1"/>  个
    </label>    
1
2
3
4
5
6
1
2
3
4
5
6
URL参数

a.参数:(orderId=${o.id})
        th:href="@{'/details/'+${user.login}(orderId=${o.id})}"    
1
2
1
2
表达式基本对象:

    #ctx:context object
    #root或者#vars
    #locale
    #httpServletRequest
    #httpSession
1
2
3
4
5
1
2
3
4
5
表达式功能对象:

    #dates:java.util.Date的功能方法类。
    #calendars:类似#dates,面向java.util.Calendar
    #numbers:格式化数字的功能方法类。
    #strings:字符串对象的功能类,contains,startWiths,prepending/appending等等。
    #objects:对objects的功能类操作。
    #bools:对布尔值求值的功能方法。
    #arrays:对数组的功能类方法。
    #lists:对lists功能类方法
    #sets
    #maps
    #aggregates:对数组或者集合创建聚合的功能方法,
    th:text="${#aggregates.sum(o.orderLines.{purchasePrice * amount})}"

    #messages:在变量表达式中获取外部信息的功能类方法。
    #ids:处理可能重复的id属性的功能类方法。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
条件操作:

    (if)?(then):满足条件,执行then。
    (if)?(then):(else)
    (value)?:(defalutValue)   
1
2
3
1
2
3
标签

    th:text="${data}",将data的值替换该属性所在标签的body。字符常量要用引号,比如th:text="'hello world'",th:text="2011+3",th:text="'my name is '+${user.name}"
    th:utext,和th:text的区别是"unescaped text"。
    th:with,定义变量,th:with="isEven=${prodStat.count}%2==0",定义多个变量可以用逗号分隔。
    th:attr,设置标签属性,多个属性可以用逗号分隔,比如th:attr="src=@{/image/aa.jpg},title=#{logo}",此标签不太优雅,一般用的比较少。
    th:[tagAttr],设置标签的各个属性,比如th:value,th:action等。
    可以一次设置两个属性,比如:th:alt-title="#{logo}"
    对属性增加前缀和后缀,用th:attrappend,th:attrprepend,比如:th:attrappend="class=${' '+cssStyle}"
    对于属性是有些特定值的,比如checked属性,thymeleaf都采用bool值,比如th:checked=${user.isActive}
    th:each, 循环,<tr th:each="user,userStat:${users}">,userStat是状态变量,有 index,count,size,current,even,odd,first,last等属性,如果没有显示设置状态变量,thymeleaf会默 认给个“变量名+Stat"的状态变量。
    th:if or th:unless,条件判断,支持布尔值,数字(非零为true),字符,字符串等。
    th:switch,th:case,选择语句。 th:case="*"表示default case。
    th:fragment,th:include,th:substituteby:fragment为片段标记,指定一个模板内一部分代码为一个片段,然后在其它的页面中用th:include或th:substituteby进行包含。
    包含的格式为,格式内可以为表达式,比如th:include="footer::$(user.logined)?'logined':'notLogin'":
    "templatename::fragname",指定模板内的指定片段。
    "templateName::[domselector]",指定模板的dom selector,被包含的模板内不需要th:fragment.
    ”templatename",包含整个模板。
    th:include和th:substituteby的区别在于前者包含片段的内容到当前标签内,后者是用整个片段(内容和上一层)替换当前标签(不仅仅是标签内容)。
    th:remove="all|body|tag|all-but-first",一般用于将mock数据在真实环境中移除,all表示移除标签以及标签内容,body只移除内容,tag只移除所属标签,不移除内容,all-but-first,除第一条外其它不移除。

    由 于一个标签内可以包含多个th:x属性,其先后顺序为:include,each,if/unless/switch/case,with,attr /attrprepend/attrappend,value/href,src ,etc,text/utext,fragment,remove。

    内联文本:[[...]]内联文本的表示方式,使用时,必须先用th:inline="text/javascript/none"激活,th:inline可以在父级标签内使用,甚至作为body的标签。内联文本尽管比th:text的代码少,但是不利于原型显示。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
内联js:
    <scriptth:inline="javascript">
    /*<![CDATA[*/
    ...
    var username = /*[[${sesion.user.name}]]*/ 'Sebastian';
    ...
    /*]]>*/
    </script>

    js附加代码:
    /*[+
    var msg = 'This is a working application';
    +]*/

    js移除代码:
    /*[- */
    var msg = 'This is a non-working template';
    /* -]*/
    模板缓存:
    1、指定特定的缓存:
    templateResolver.setCacheable(false);
    templateResolver.getCacheablePatternSpec().addPattern("/users/*");
    2、清除缓存:
    templateEngine.clearTemplateCache();
    templateEngine.clearTemplateCacheFor("/users/userList"); 

  

原文地址:https://www.cnblogs.com/airycode/p/6511461.html