Thymeleaf入门

常用属性

  1. th:action,与form表单的action效果相同

    
    <form th:action="@{/login}"/>
  2. th:each:遍历

    
    <li th:each="book : ${books}" th:text="${book.title}">
    xxdxxxxxxx
    </li>
  3. th:href :超链接

    
    <a th:href="@{/list}"></a>
  4. th:if :条件判断

    
    <div th:if="${rowStat.index} == 0">... do something ...</div>
  5. th:fragment: 定义可以复用的片段,通常与th:include,th:replace一同使用

    例如,在页面中定义了如下片段:
    
    <div th:fragment="copy">
        <p>这是可以复用的片段</p>
    </div>
    
    如果要在指定位置引用它:
    
    <div th:insert="~{footer :: copy}">
    </div>
  6. th:field: 绑定表单字段,通常与th:object一同使用

    <form id="login-form" th:action="@{/login}" th:object="${loginBean}">...
    
    <input type="text" value="" th:field="*{username}"></input>
    
    <input type="text" value="" th:field="*{user[0].username}"></input>
    
    </form>
  7. th:src :用于外部资源的引入

    <script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

常用语法

  1. 变量表达式

    语法:${...}
    
    <span th:text="${book.author.nam}">
  2. 消息表达式

    语法: #{...}
    
    <table>
        ...
        <th th:text="#{header.address.city}">...</th>
        <th th:text="#{header.address.country}">...</th>
        ...
    </table>
  3. 选择表达式

    语法: *{...}
    
    <div th:object="${book}">
        ...
        <span th:text="*{title}">...</span>
        ...
    </div>
    
    // div中先取出book对象,span中再取出book对象中的title属性
    
    与变量表达式的区别是:选择表达式是在当前选择的对象中取值,而不是整个上下文变量映射上执行
  4. 链接表达式

    1. 语法:@{...}
    
    2. 链接表达式可以是相对的,在这种情况下,应用程序上下文将不会作为URL的前缀
    
    <a th:href="@{../documents/report}">...</a>
    
    3. 与服务器相对,同样没有持程序上下文前缀
    
    <a th:href="@{~/contents/main}">...</a>
    
    4. 和协议相对(类似绝对URl,但浏览器将使用在显示的页面中的相同的HTTp或HTTPS协议)
    
    <a th:href="@{//static.mycompany.com/res/initial}">...</a>
    
    5. Link表达式可以是绝对的
    
    <a th:href="@{http://www.mycompany.com/main}">...</a>
  5. 分段表达式

    1. 语法: th:insert 或 th:replace
    
    例如,在页面中定义了如下片段:
    
    <div th:fragment="copy">
        <p>这是可以复用的片段</p>
    </div>
    
    如果要在指定位置引用它:
    
    <div th:insert="~{footer :: copy}">
    </div>
  6. 字面量(文字)

    1. 文本
    
    <span th:text="'这是要显示的内容'"></span>
    
    单引号代表其为文本
    
    2. 数字
    
    <span th:text="2013"></span>
    
    <span th:text="2013 + 2"></span>
    
    3. 布尔
    
    <div th:if="${user.isAdmin() == false}">...</div>
    
    4. null
    
    <div th:if="${user.something == null}">...</div>
    
  7. 算术操作

    +,-,*,/,%
    
    <div th:with="isEven=(${prodStat.count} % 2 == 0)">...</div>
  8. 比较运算符

    比较: >,<,>=<,<=(gt,lt,ge,le)
    
    <ul data-th-if="${page.totalPages le 7}">...
    
    等价: ==,!=(e1,ne)
    <div data-th-selected="${i eq page.size}"></div>
  9. 条件运算符

    <tr th:class="${row.even}? 'even' : 'odd'">...</tr>
    
    无操作:_
    <tr th:class="${user.name}?:_">...</tr>
    
  10. 设置任意属性值

    <form action="" th:attr="action=@{/subscribe}">
    <input type="submit" value="" th:attr="value=#{subscribe.submit}"/>
    </form>
    
    可以直接设置
    
    <form action="" th:action="@{/subscribe}">
    <input type="submit" value="" th:value="#{subscribe.submit}"/>
    </form>
  11. 迭代器

    基本的迭代: th:each
    
    <li th:each="book : ${books}" th:text="${book.title}">
    xxdxxxxxxx
    </li>
    
    状态变量
    
    index,count,size,current,even/odd,first,last
    
    <tr th:each="prod,iterStat : ${prods}" th:class="${iterStat.odd}? 'odd'">
    ...
    <tr/>
  12. 条件语句

    th:if,th:unless
    
    <a href="comments.html"
        th:href="..."
        th:if="${not #list.isEmpty(prod.comments)}">view</a>
    
    switch
    
    <div th:switch="${user.role}">
    <p th:case="'admin'">User is an admin</p>
    <p th:case="#{roles.manager}">User is an manager</p>
    <p th:case="'*'">User is an person</p>
    </div>

模板布局

  1. 定义和引用片段

    例如,在页面中定义了如下片段:
    
    <div th:fragment="copy">
        <p>这是可以复用的片段</p>
    </div>
    
    如果要在指定位置引用它:
    
    <div th:insert="~{footer :: copy}">
    </div>
    
    不使用 th:fragment
    <div id="copy-section">
        <p>这是可以复用的片段</p>
    </div>
    
    <div th:insert="~{footer :: #copy-section}">
    </div>
  2. th:insert,th:replace,th:include三者之间的区别
    1. th:insert: 它将简单地插入指定的片段作为正文的主标签
    2. th:replace: 用指定的实际片段来替换主标签
    3. th:include: 类似于th:include,但不是插入片段它只插入此片段的内容(3.x版本之后,不建议使用)

属性优先级

  1. 挡在同一个标签中写入多个th:*属性时,会发生什么?

    <li th:each="book : ${books}" th:text="${book.title}">
    xxdxxxxxxx
    </li>
  2. 属性优先级列表

顺序功能属性
1模块包含th:include,th:replace
2模块循环th:each
3条件判断th:if,th:unless,th:switch,th:case
4局部变量th:object,th:with
5通用属性修改th:attr,th:attrprepend,th:attrappend
6特殊属性修改th:value,th:href,th:src…
7文本显示th:text,th:utext
8模块定义th:fragment
9模块移除th:remove

注释

  1. 标准 HTML/XML注释
  2. Thymeleaf 解析器级注释快

    <!--/*和*/-->
    
    会在解析的时候删除注释的所有内容
  3. 原型注释快

    • 当模板静态打开时(比如原型设计),原型注释块所注释的代码将被注释,而在模板执行时,这些注释的代码,就能被显示出来
      
      语法:<!--/*/需要注释的内容/*/-->

内联

  1. 内联表达式

    [[...]]或[(...)]分别对应于th:text和th:utext
    
    [[...]](th:text) 会对内容中的特殊符号进行转义
  2. 禁用内联表达式

    
    <p th:inline="none">[[1,2,3],[4,5]]</p>
  3. JavaScript内联

    
    <script th:inline="javascript">
    ...
    var username = /*[[${session.user.name}]*/ "xxxx"]
    ...
    </script>
  4. CSS内联

    
    <style th:inline="css">
        .[[${classname}]]{
            text-align:[[${align}]];
        }
    </style>

表达式基本对象

  1. ctx: 上下文对象

    ${#ctx.locale}
    ${#ctx.variableNames}
    
    ${#ctx.request}
    ${#ctx.response}
    ${#ctx.session}
    ${#ctx.servletContext}
    
    ${#locale}
  2. request/session等属性

    1. param:用于检索请求参数
    2. session:用于检索上session属性
    3. application:用于检索application/servlet上下文属性
    ${param.foo} 
    ${param.size()}
    ${param.isEmpty()}
    ${param.containsKey('foo')} 
    
    ${session.foo} 
    ${session.size()}
    ${session.isEmpty()}
    ${session.containsKey('foo')} 
    
    {application.foo} 
    ${application.size()}
    ${application.isEmpty()}
    ${application.containsKey('foo')} 
  3. Web上下文对象

    1. #request:直接访问当前请求关联的javax.servlet.http.HttpServletRequest对象
    2. #session:直接访问当前请求关联的javax.servlet.http.HttpSession对象
    3. #servletContext:直接访问当前请求关联的javax.servlet.http.ServletContext对象
    ${#request.getAttribute('foo')}
    ${#request.getParameter('foo')}
    ${#request.getContextPath()}
    ${#request.getRequestName()}
    
    ${#session.getAttribute('foo')}
    ${#session.id}
    ${#session.lastAccessedTime}
    
    ${servletContext.getAttribute('foo')}
    ${#servletContext.getContextPath()}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值