Thymeleaf学习笔记

未完成,不定时更新

 

1.Thymeleaf的方言

<span th:text="....">
<span data-th-text="....">

举例说明

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf Demo</title>
</head>
<body>
	<p th:text="#{home.welcome}">hello world:thymeleaf</p>
</body>
</html>

表达式

消息表达式:  #{...}

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf Demo</title>
</head>
<body>
	<table>
		 <tr>
			<th th:text="#{header.address.city}">xi'an</th>
			<th th:text="#{header.address.country}">china</th>
		 </tr>
	</table>
</body>
</html>

  选择表达式:*{ .... }

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

该表达式的含义是 选择 book变量的title属性

与变量表达式的区别:它们是在当前选择的对象(book)基础之上再进行选择title属性(book.title),而不是整个上下文变量映射执行

链接表达式:@{ .... }

<a th:href="@{http://www.baidu.com}">百度</a>

 分段表达式 : th:insert 或th:replace

 <div th:fragment ="base">
        &copy;2018<a href="www.baidu.com">百度</a>
    </div>
    <div th:insert="~{footer :: base}"></div>

字面量

    <p>
        文本,单引号 <span  th:text="'hello world '"></span>
        数字<span  th:text="21*12">-1</span>
    </p>
    <div th:if="${user.isAdmin()} == false"></div>
    <div th:if="${user.create()} == null"></div>

设置属性值: th:attr

 <form action="subscribe.html" th:attr="action=@{/subscribe}">
    <fieldset>
        <input type="text" name="email"  />        
        <input type="submit" value = "Subscribe" th:attr="value=#{subscribe.submit}"/>     
   </fieldset>
</form>

迭代器: th:each

<ul>
        <li th:each="book :${bools}" th:text="${book.title}"> </li>
    </ul>
    <!-- 状态变量  index,count,size,current,even/odd,first,last-->
    <table>
        <tr>
            <th>NAME</th>
            <th>PRICE</th>
            <th>IN STOCK</th>
            <th>test</th>
        </tr>    
        <tr th:each="prod,iterStat :${prods}" th:class="${iterStat.odd}? 'odd'">
            <td th:text="${prod.name}">Onions</td>
            <td th:text="${prod.price}">2.41</td>
            <td th:text="${prod.inStock} ? #{true} : #{false}">yes</td>
            <td th:text="${iterStat.odd} ? #{true} : #{false}">yes</td>
        </tr>
    </table>
    <ol>
         <li>Map循环:  
            <div th:each="mapS:${map}">  
            <div th:text="${mapS}"></div>  
            </div>  
        </li>  
        <li>数组循环:  
            <div th:each="arrayS:${arrays}">  
            <div th:text="${arrayS}"></div>  
            </div>  
        </li>  
    </ol>

条件语句: th:if ,th:unless 

<a href="comments.html"
	   th:href="@{/product/comments({prodId=${prod.id}})}"
	   th:if="${not #lists.isEmpty(prod.comments)}">view</a>

 

2.常见问题

1.   ${}  、#{} 、 *{}  的比较

    ${ }

    变量表达式(美元表达式),用于访问容器上下文环境中的变量,功能同jstl中${}

    #{ }

     消息表达式(井号表达式,资源表达式)。通常与th:text属性一起使用,指明声明了th:text的标签的文本是#{}中的key          所对应的value,而标签内的文本将不会显示。

   * { }

     选择表达式(星号表达式)。选择表达式与变量表达式有一个重要的区别:选择表达式计算的是选定的对象,而不是整       个环境变量映射

 

 

 

 

 

 

参考文章

 http://www.cnblogs.com/hjwublog/p/5051632.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值