Thymeleaf模版引擎

引入thymeleaf

<html xmlns:th="http://www.thymeleaf.org">

 

一、变量表达式 (   ${...}   )

 

<span th:text="${book.author.name}"></span>

 

二、消息表达式 (    #{...}    )

 

<th th:text="#{header.address.city}"></th>

 

三、选择表达式 (    *{...}     )

 

    <div th:object="${book}">
        <span th:text="*{title}"></span>
    </div>

 

四、链接表达式 (    @{...}    )

 

    <!-- 链接表达式可以是相对的,在这种情况下,应用程序上下文将不会作为URL的前缀 -->
    <a th:href="@{../devsoft/thymeleaf}"></a>
    <!-- 也可以是服务器相对的(同样没有应用程序上下文前缀) -->
    <a th:href="@{~/content/main}"></a>
    <!-- 和协议相对(就像绝对url,但浏览器将使用在显示的页面中使用的相同的HTTP或HTTPS协议) -->
    <a th:href="@{//static.com}"></a>
    <!-- url绝对 -->
    <a th:href="@{http://www.baidu.com}"></a>

 

五、设置属性值

 

<input th:attr="value=#{book.name}">
<input th:value="#{book.name}">

 

六、迭代器 ( 遍历 -> th:each )

 

<li th:each="book : ${book}" th:text="${book.title}"></li>
// 状态变量
index、count、size、current、even/odd、first、last

 

七、条件语句

 

<!-- th:if="${ aa == bb}" -->
<!-- th:unless="${aa == bb}" -->
<!-- th:switch -->
<div th:switch="${user.name}">
   <p th:case="Tom">success</p>
   <p th:case="#{roles.manager}">success</p>
   <p th:case="*">success</p>
</div>

 

八、模板布局

 

  <div id="copy">
        &copy; test
    </div>
    <!--
    <div th:fragment="copy">
        &copy; test
    </div>
    -->
    <div th:insert="~{index :: #copy}">123</div>
    <div th:replace="~{index :: #copy}">123</div>
    <div>123</div>

 

九、属性优先级

 

 

十、内联表达式

    <!-- th:utext 对应 [(...)] -->
    <p>The message is "[(${msg})]"</p> --> <p>The message is "This is <b>greet!</b>"</p>
    <!-- th:text 对应 [[...]] -->
    <p>The message is "[[${msg}]]"</p> --> <p>The message is "This is <b>greet!</b>"</p>

    <!-- 只想使用[()]、[[]]这两个符号,不启用内联表达式,在标签内使用th:inline=none -->
    <p th:inline="none"></p>

 

十一、和SpringBoot集成

 

// 引入thymeleaf依赖
compile('org.springframework.boot:spring-boot-starter-thymeleaf:2.0.3.RELEASE')
#application.properties配置
#thymelea模板配置
#spring.thymeleaf.prefix=classpath:/templates/
#spring.thymeleaf.suffix=.html
spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
#热部署文件,页面不产生缓存,及时更新
spring.thymeleaf.cache=false
spring.resources.chain.strategy.content.enabled=true
spring.resources.chain.strategy.content.paths=/**

 

十二、

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值