本文只适用于不会Java对HTML语言有基础的程序员们,是浏览了各大博客后收集整理,从新编辑的一篇文章,但愿能对你们有所帮助。最后本文若是有哪里写错的,但愿各位大神们可以批评指正,谢谢你们!html
对于Thymeleaf,网上特别官方的解释无非就是:网站或者独立应用程序的新式的服务端java模板引擎,能够执行HTML,XML,JavaScript,CSS甚至纯文本模板。这个解释没有任何问题,它确实是创建在Java的基础之上的,可是像我这种只会前端不懂Java的人,其实也能够运用它。了解angular的人在看到Thymeleaf就会感到惊喜,它们在形式上实际上是比较类似的。那么,Thymeleaf须要从那里看起?做为Java小白,刚开始看了网上那么多Thymeleaf文章也看不出个因此然,今天好不容易才整理出头绪,接下来就开始切入正题:前端
<td th:text="${food.name}">noodles</td>
如上图,后台传出的food.name会将静态数据“noodles”替换掉,若访问静态页面,则显示数据“noodles”。是否是和angular很像?下面咱们就来换一种方式,不一样于其余博客上的方式来介绍Thymeleaf。java
固然,首先你们要先知道th简单表达式:jquery
1、th简单表达式:程序员
① ${...} 变量表达式:web
<input type="text" name="userName" value="Beyrl" th:value="${user.name}" />
上述代码为引用user对象的name属性值。session
② *{...} 选择表达式:less
<div th:object="${session.user}"> <p>Nationality: <span th:text="*{nationality}">XXXX</span>.</p> </div>
选择表达式通常跟在th:object后,直接选择object中的属性。post
③ #{...} 消息文字表达式:学习
<p th:utext="#{home.welcome}">Welcome to our grocery store!</p>
④ @{...} 连接url表达式:
<a href="details.html" th:href="@{/webPage/details(orderId=${o.id})}">view</a>
@{……}支持决定路径和相对路径。其中相对路径又支持跨上下文调用url和协议的引用(//code.jquery.com/jquery-2.0.3.min.js)。
<img src="../../webPage/food/images/pizza.jpg" th:src="@{${path}}" alt="披萨" />
当理解了这四个表达式后,我就信心满满的去向下看文档,而后我发现我看不懂了。。。由于我不理解什么是th:field='';th:action='';诸如此类的好多好多,后来在一个博客上看到这一类的是所谓的Thymeleaf的属性,或者是经常使用的th:标签,下面咱们就来整理学习一下这些标签:
这是在一个博客上看到的整理的较全的图片,还有一个更全的,那个太多了,会吓到初学者,不知道大家会不会,反正我是被吓到了。。。
下面咱们会详细介绍一些经常使用的标签:
二、th经常使用标签:
1.th:id:
相似html标签中的id属性。
<div class="student" th:id = "food+(${pizza.index}+1)"></div>
2.th:text:与th:utext:
即文本显示,可对表达式或变量求值,并将结果显示在其被包含的HTML标签内,替换原有HTML文本。这里须要与th:utext:区分开,th:text:例子以下:
若 restraunt.welcome=welcome to our <b>delicious</b>restaurant! 那么,用 <p h:text="#{restaurantt.welcome}"></p> 解析的结果为: welcome to our <b>delicious</b>restaurant! ,
也就是说,会输出 welcome to our <b>delicious</b>restaurant</> 固然,咱们是不会但愿页面上出现<和e>的,这时候,咱们就须要使用th:utext:来进行转义,即用 <p h:utext="#{restaurant.welcome}"></p>
因此最终输出的结果为:welcome to our delicious restaurant!
3.th:object:
用于表单数据对象绑定,将表单绑定到后台controller的一个JavaBean参数,常与th:field一块儿使用进行表单数据绑定。选择表达式通常跟在th:object后,直接取object中的属性。
这里有一个须要注意的点:*{...}表达式的值是在选定的对象而不是整个context的map。也就是说,若是没有选定的对象,*{...}和${...}没有区别,请看下面的例子:
<div th:object="${session.user}"> <p>姓名:<span th:text="*{Name}">noodles</span></p> <p>年龄:<span th:text="*{age}">24</span></p> <p>国籍:<span th:text="*{nationlity}">中国</span></p> </div>
上面这段代码至关于:
<div> <p>姓名:<span th:text="${session.user.Name}">noodles</span></p>
<p>年龄:<span th:text="${session.user.age}">24</span></p>
<p>国籍:<span th:text="${session.user.nationlity}">中国</span></p></div>
4.th:field:上面提到了一个新标签,th:field:,经常使用于表单字段绑定。一般与th:object一块儿使用。 属性绑定、集合绑定。
<form th:action="@{/bb}" th:object="${user}" method="post" th:method="post"> <input type="text" th:field="*{name}"/> <input type="text" th:field="*{msg}"/> <input type="submit"/> </form>
5.th:action:定义后台控制器路径,相似<form>标签的action属性。
<form action="subscribe.html" th:action="@{/subscribe}">
6.th:href:定义超连接,相似<a>标签的href 属性。value形式为@{/logout}.
<!-- 输出: 'http://localhost:8080/gtvg/order/details?orderId=3' --> <a href="details.html" th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a> <!-- 输出: '/gtvg/order/details?orderId=3' --> <a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a> <!-- 输出: '/gtvg/order/3/details' --> <a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
7.th:src:用于外部资源引入,相似于<script>标签的src属性,常与@{}一块儿使用。
<script th:src="@{/js/jquery/jquery-2.4.min.js}">
8.th:value:用于标签赋值,相似<option>标签的value属性。
<option th:value="soup">soup</option> <input id="msg" th:value="${msg}" />
9.th:if or th:unless:条件判断,支持布尔值,数字(非零为true),字符,字符串等.
<div th:if="${restaurant.index} == 0">... I love eating(do something at here) ...</div> <span th:if="${food.price lt 100}" class="offer">Special desert!</span> /*不能用"<",">"等符号,要用"lt"等替代*/ <select class='form-control' name="skill[4].proficiency"> <option >掌握程度</option> <option th:if="${skill.level eq '通常'}" th:selected="selected">通常</option> <option th:if="${skill.level eq '熟练'}" th:selected="selected">熟练</option> <option th:if="${skill.level eq '精通'}" th:selected="selected">精通</option> </select>
这里有两个须要注意的点:先看下面两行代码,
<div th:if="${user.isAdmin()} == false"> ...
<div th:if="${user.isAdmin() == false}"> ...
在这个例子中,==false是写在了\({...}的外边,因此使Thymeleaf自己在支持它,若是写在了\){...}的里边,则变为由OGNL或SpringEL库来支持它。(***这里目前我还未明白是什么意思,但愿明白的大神能告诉我这个问题***)
而null值也能够这么使用:
<div th:if="${variable.something} == null"> ...
th:if不光可使用布尔值,如下规则均可以:
● 若是值为null,th:if将为false
th:if还有一个互逆的表达式为th:unless,还继续用以前的例子做一个演示:
<a href="comments.html" th:href="@{/comments(prodId=${prod.id})}" th:unless="${#lists.isEmpty(prod.comments)}">查看</a>
下面的是一个th:if 例子,你们能够照着套一下。
<table> <tr> <th>食物名称</th> <th>食物价格</th> <th>可现作</th> <th>食客评价</th> </tr> <tr th:each="prod:${prods}"> <td th:text="${prod.name}">醋溜土豆丝</td> <td th:text="${#numbers.formatDecimal(prod.price,0,2)}">2.41</td> <td th:text="${prod.isReady}?#{true}:#{false}">yes</td> <td> <span th:text=${#lists.size(prod.comments)}>2</span>个评价 <a href="comments.html" th:href="@{/product/comments(prodId=${prod.id})}" th:if="${not #lists.isEmpty(prod.comments)}">查看</a> </td> </tr> </table>
若是产品有评论,那么咱们就建立一个跳转到评论页面的超连接,而且使用产品ID做为参数。
10.th:switch 和th:case:选择语句。 th:case="*"表示default case。注意:一旦一个th:case被判断为真,那么其余的同等级的th:case都将被判断为假
<div th:switch="${user.role}"> <p th:case="'admin'">超级管理员用户</p> <p th:case="#{roles.manager}">管理员用户</p> <p th:case="*">其余用户</p> </div>
11.th:with:定义变量,th:with="isEven=${prodStat.count}%2 == 0",定义多个变量能够用逗号分隔。
<div th:with="firstPer=${persons[0]}"> <p> The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>. </p> </div>
当th:with被处理,firstPer变量建立一个局部变量和变量添加到map自上下文,以便它是用于评估和其余上下文中声明的变量从开始,但只有包含< div >标记的范围内。
div th:with="firstPer=${persons[0]},secondPer=${persons[1]}">
<p> The name of the first person is <span th:text="${firstPer.name}">Julius Caesar</span>. </p> <p> But the name of the second person is <span th:text="${secondPer.name}">Marcus Antonius</span>. </p> </div>
th:with属性容许重用变量定义在相同的属性:
<div th:with="company=${user.company + ' Co.'},account=${accounts[company]}">...</div>
12.th:remove:移除除了第一个外的静态数据,用第一个tr标签进行循环迭代显示:
<tbody th:remove="all-but-first"> //将后台传出的 productList 的集合进行迭代,用product参数接收,经过product访问属性值 <tr th:each="product:${productList}"> //用count进行统计,有顺序的显示 <td th:text="${productStat.count}">1</td> <td th:text="${product.description}">Red Chair</td> <td th:text="${'$' + #numbers.formatDecimal(product.price, 1, 2)}">$123</td> <td th:text="${#dates.format(product.availableFrom, 'yyyy-MM-dd')}">2014-12-01</td> </tr> <tr> <td>White table</td> <td>$200</td> <td>15-Jul-2013</td> </tr> <tr> <td>Reb table</td> <td>$200</td> <td>15-Jul-2013</td> </tr> <tr> <td>Blue table</td> <td>$200</td> <td>15-Jul-2013</td> </tr> </tbody>