欧美一级特黄大片做受成人-亚洲成人一区二区电影-激情熟女一区二区三区-日韩专区欧美专区国产专区

SpringBoot中Thymeleaf如何使用

本篇文章給大家分享的是有關(guān)SpringBoot 中Thymeleaf如何使用,小編覺(jué)得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說(shuō),跟著小編一起來(lái)看看吧。

成都創(chuàng)新互聯(lián)公司專注于大理州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供大理州營(yíng)銷型網(wǎng)站建設(shè),大理州網(wǎng)站制作、大理州網(wǎng)頁(yè)設(shè)計(jì)、大理州網(wǎng)站官網(wǎng)定制、微信小程序開發(fā)服務(wù),打造大理州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供大理州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。

  1. 標(biāo)準(zhǔn)表達(dá)式語(yǔ)法

  • 變量表達(dá)式

變量表達(dá)式即 OGNL 表達(dá)式或 Spring EL 表達(dá)式(在 Spring 術(shù)語(yǔ)中也叫 model attributes)。如下所示:

${session.user.name}

它們將以HTML標(biāo)簽的一個(gè)屬性來(lái)表示:

<span th:text="${book.author.name}">
<li th:each="book : ${books}">
  • 選擇(星號(hào))表達(dá)式

選擇表達(dá)式很像變量表達(dá)式,不過(guò)它們用一個(gè)預(yù)先選擇的對(duì)象來(lái)代替上下文變量容器(map)來(lái)執(zhí)行,如下:

*{customer.name}

被指定的 object 由 th:object 屬性定義:

<div th:object="${book}">
  ...
  <span th:text="*{title}">...</span>
  ...
</div>
  • 文字國(guó)際化表達(dá)式

文字國(guó)際化表達(dá)式允許我們從一個(gè)外部文件獲取區(qū)域文字信息(.properties),用 Key 索引 Value,還可以提供一組參數(shù)(可選).

#{main.title}  
#{message.entrycreated(${entryId})}

可以在模板文件中找到這樣的表達(dá)式代碼:

<table>
  ...
  <th th:text="#{header.address.city}">...</th>
  <th th:text="#{header.address.country}">...</th>
  ...
</table>
  • URL表達(dá)式

URL 表達(dá)式指的是把一個(gè)有用的上下文或回話信息添加到 URL,這個(gè)過(guò)程經(jīng)常被叫做 URL 重寫。

@{/order/list}

URL還可以設(shè)置參數(shù):

@{/order/details(id=${orderId})}

相對(duì)路徑:

@{../documents/report}

讓我們看這些表達(dá)式:

<form th:action="@{/createOrder}">
<a href="main.html" th:href="@{/main}">
  • 變量表達(dá)式和星號(hào)表達(dá)式有什么區(qū)別?

如果不考慮上下文的情況下,兩者沒(méi)有區(qū)別;星號(hào)語(yǔ)法評(píng)估在選定對(duì)象上表達(dá),而不是整個(gè)上下文 什么是選定對(duì)象?就是父標(biāo)簽的值,如下:

<div th:object="${session.user}">
  <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
  <p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
  <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>

這是完全等價(jià)于:

<div th:object="${session.user}">
  <p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
  <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
  <p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>

當(dāng)然,美元符號(hào)和星號(hào)語(yǔ)法可以混合使用:

<div th:object="${session.user}">
	  <p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
  	  <p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
      <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
  </div>
  • 表達(dá)式支持的語(yǔ)法

    • If-then:(if) ? (then)

    • If-then-else:(if) ? (then) : (else)

    • Default: (value) ?:(defaultvalue)

    • 所有這些特征可以被組合并嵌套:

    • 比較(Comparators):>, <, >=, <= (gt, lt, ge, le)

    • 等值運(yùn)算符(Equality operators):==, != (eq, ne)

    • 二元運(yùn)算符(Binary operators):and, or

    • 布爾否定(一元運(yùn)算符)Boolean negation (unary operator):!, not

    • 二元運(yùn)算符(Binary operators):+, -, *, /, %

    • 減號(hào)(單目運(yùn)算符)Minus sign (unary operator):-

    • 字符串連接(String concatenation):+

    • 文本替換(Literal substitutions):|The name is ${name}|

    • 文本文字(Text literals): 'one text', 'Another one!',…

    • 數(shù)字文本(Number literals): 0, 34, 3.0, 12.3,…

    • 布爾文本(Boolean literals): true, false

    • 空(Null literal):null

    • 文字標(biāo)記(Literal tokens): one, sometext, main,…

    • 字面(Literals)

    • 文本操作(Text operations)

    • 算術(shù)運(yùn)算(Arithmetic operations)

    • 布爾操作(Boolean operations)

    • 比較和等價(jià)(Comparisons and equality)

    • 條件運(yùn)算符Conditional operators)

      	'User is of type ' + (${user.isAdmin()} ? 'Administrator' : (${user.type} ?: 'Unknown'))

  1. 常用th標(biāo)簽有哪些?

關(guān)鍵字功能介紹案例
th:id替換id<input th:id="'xxx' + ${collect.id}"/>
th:text文本替換<p th:text="${collect.description}">description</p>
th:utext支持html的文本替換<p th:utext="${htmlcontent}">conten</p>
th:object替換對(duì)象<div th:object="${session.user}">
th:value屬性賦值<input th:value="${user.name}" />
th:with變量賦值運(yùn)算<div th:with="isEven=${prodStat.count}%2==0"></div>
th:style設(shè)置樣式th:
th:onclick點(diǎn)擊事件th:onclick="'getCollect()'"
th:each屬性賦值tr th:each="user,userStat:${users}">
th:if判斷條件<a th:if="${userId == collect.userId}" >
th:unless和th:if判斷相反<a th:href="@{/login}" th:unless=${session.user != null}>Login</a>
th:href鏈接地址<a th:href="@{/login}" th:unless=${session.user != null}>Login</a> />
th:switch多路選擇 配合th:case 使用<div th:switch="${user.role}">
th:caseth:switch的一個(gè)分支<p th:case="'admin'">User is an administrator</p>
th:fragment布局標(biāo)簽,定義一個(gè)代碼片段,方便其它地方引用<div th:fragment="alert">
th:include布局標(biāo)簽,替換內(nèi)容到引入的文件<head th:include="layout :: htmlhead" th:with="title='xx'"></head> />
th:replace布局標(biāo)簽,替換整個(gè)標(biāo)簽到引入的文件<div th:replace="fragments/header :: title"></div>
th:selectedselected選擇框 選中th:selected="(${xxx.id} == ${configObj.dd})"
th:src圖片類地址引入<img class="img-responsive" alt="App Logo" th:src="@{/img/logo.png}" />
th:inline定義js腳本可以使用變量<script type="text/javascript" th:inline="javascript">
th:action表單提交的地址<form action="subscribe.html" th:action="@{/subscribe}">
th:remove刪除某個(gè)屬性<tr th:remove="all"> 1.all:刪除包含標(biāo)簽和所有的孩子。2.body:不包含標(biāo)記刪除,但刪除其所有的孩子。3.tag:包含標(biāo)記的刪除,但不刪除它的孩子。4.all-but-first:刪除所有包含標(biāo)簽的孩子,除了第一個(gè)。5.none:什么也不做。這個(gè)值是有用的動(dòng)態(tài)評(píng)估。
th:attr設(shè)置標(biāo)簽屬性,多個(gè)屬性可以用逗號(hào)分隔比如th:attr="src=@{/image/aa.jpg},title=#{logo}",此標(biāo)簽不太優(yōu)雅,一般用的比較少。

還有非常多的標(biāo)簽,這里只列出最常用的幾個(gè),由于一個(gè)標(biāo)簽內(nèi)可以包含多個(gè)th:x屬性,其生效的優(yōu)先級(jí)順序?yàn)?

include,each,if/unless/switch/case,with,attr/attrprepend/attrappend,value/href,src ,etc,text/utext,fragment,remove。
  1. 幾種常用的使用方法

  • 賦值、字符串拼接

<p  th:text="${collect.description}">description</p>
<span th:text="'Welcome to our application, ' + ${user.name} + '!'">

字符串拼接還有另外一種簡(jiǎn)潔的寫法

<span th:text="|Welcome to our application, ${user.name}!|">
  • 條件判斷 if/Unless

Thymeleaf中使用th:if和th:unless屬性進(jìn)行條件判斷,下面的例子中,<a>標(biāo)簽只有在th:if中條件成立時(shí)才顯示:

<a th:if="${myself=='yes'}" > </i> </a>
<a th:unless=${session.user != null} th:href="@{/login}" >Login</a>

th:unless 于 th:if 恰好相反,只有表達(dá)式中的條件不成立,才會(huì)顯示其內(nèi)容。 也可以使用 (if) ? (then) : (else)這種語(yǔ)法來(lái)判斷顯示的內(nèi)容

  • for循環(huán)

<tr  th:each="collect,iterStat : ${collects}"> 
   <th scope="row" th:text="${collect.id}">1</th>
   <td >
      <img th:src="${collect.webLogo}"/>
   </td>
   <td th:text="${collect.url}">Mark</td>
   <td th:text="${collect.title}">Otto</td>
   <td th:text="${collect.description}">@mdo</td>
   <td th:text="${terStat.index}">index</td>
</tr>

iterStat稱作狀態(tài)變量,屬性有:

- index:當(dāng)前迭代對(duì)象的 index(從0開始計(jì)算)
- count: 當(dāng)前迭代對(duì)象的 index(從1開始計(jì)算)
- size:被迭代對(duì)象的大小
- current:當(dāng)前迭代變量
- even/odd:布爾值,當(dāng)前循環(huán)是否是偶數(shù)/奇數(shù)(從0開始計(jì)算)
- first:布爾值,當(dāng)前循環(huán)是否是第一個(gè)
- last:布爾值,當(dāng)前循環(huán)是否是最后一個(gè)
  • URL

URL 在 Web 應(yīng)用模板中占據(jù)著十分重要的地位,需要特別注意的是 Thymeleaf 對(duì)于 URL 的處理是通過(guò)語(yǔ)法 @{...} 來(lái)處理的。 如果需要 Thymeleaf 對(duì) URL 進(jìn)行渲染,那么務(wù)必使用 th:href,th:src 等屬性,下面是一個(gè)例子

<!-- Will produce 'http://localhost:8080/standard/unread' (plus rewriting) -->
 <a  th:href="@{/standard/{type}(type=${type})}">view</a>

<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>

設(shè)置背景

<div th:></div>

根據(jù)屬性值改變背景

 <div class="media-object resource-card-image"  
 th: ></div>

幾點(diǎn)說(shuō)明:

- 上例中 URL 最后的(orderId=${o.id}) 表示將括號(hào)內(nèi)的內(nèi)容作為 URL 參數(shù)處理,
該語(yǔ)法避免使用字符串拼- 接,大大提高了可讀性
- @{...}表達(dá)式中可以通過(guò){orderId}訪問(wèn) Context 中的 orderId 變量
- @{/order}是 Context 相關(guān)的相對(duì)路徑,在渲染時(shí)會(huì)自動(dòng)添加上當(dāng)前 Web 應(yīng)用的 Context 名字,
假設(shè) context 名字為 app,那么結(jié)果應(yīng)該是 /app/order
  • 內(nèi)聯(lián)js

內(nèi)聯(lián)文本:[[…]] 內(nèi)聯(lián)文本的表示方式,使用時(shí),必須先用th:inline="text/javascript/none"激活,th:inline可以在父級(jí)標(biāo)簽內(nèi)使用,甚至作為 body 的標(biāo)簽。內(nèi)聯(lián)文本盡管比th:text的代碼少,不利于原型顯示。

<script th:inline="javascript">
/*<![CDATA[*/
...
var username = /*[[${sesion.user.name}]]*/ 'Sebastian';
var size = /*[[${size}]]*/ 0;
...
/*]]>*/
</script>

js 附加代碼:

/*[+
var msg = 'This is a working application';
+]*/

js 移除代碼:

/*[- */
var msg = 'This is a non-working template';
/* -]*/
  • 內(nèi)嵌變量

為了模板更加易用,Thymeleaf 還提供了一系列 Utility 對(duì)象(內(nèi)置于 Context 中),可以通過(guò) # 直接訪問(wèn):

dates : java.util.Date的功能方法類。
calendars : 類似#dates,面向java.util.Calendar
numbers : 格式化數(shù)字的功能方法類
strings : 字符串對(duì)象的功能類,contains,startWiths,prepending/appending等等。
objects: 對(duì)objects的功能類操作。
bools: 對(duì)布爾值求值的功能方法。
arrays:對(duì)數(shù)組的功能類方法。
lists: 對(duì)lists功能類方法
sets
maps

下面用一段代碼來(lái)舉例一些常用的方法:

dates

/*
 * Format date with the specified pattern
 * Also works with arrays, lists or sets
 */
${#dates.format(date, 'dd/MMM/yyyy HH:mm')}
${#dates.arrayFormat(datesArray, 'dd/MMM/yyyy HH:mm')}
${#dates.listFormat(datesList, 'dd/MMM/yyyy HH:mm')}
${#dates.setFormat(datesSet, 'dd/MMM/yyyy HH:mm')}

/*
 * Create a date (java.util.Date) object for the current date and time
 */
${#dates.createNow()}

/*
 * Create a date (java.util.Date) object for the current date (time set to 00:00)
 */
${#dates.createToday()}

strings

/*
 * Check whether a String is empty (or null). Performs a trim() operation before check
 * Also works with arrays, lists or sets
 */
${#strings.isEmpty(name)}
${#strings.arrayIsEmpty(nameArr)}
${#strings.listIsEmpty(nameList)}
${#strings.setIsEmpty(nameSet)}

/*
 * Check whether a String starts or ends with a fragment
 * Also works with arrays, lists or sets
 */
${#strings.startsWith(name,'Don')}                  // also array*, list* and set*
${#strings.endsWith(name,endingFragment)}           // also array*, list* and set*

/*
 * Compute length
 * Also works with arrays, lists or sets
 */
${#strings.length(str)}

/*
 * Null-safe comparison and concatenation
 */
${#strings.equals(str)}
${#strings.equalsIgnoreCase(str)}
${#strings.concat(str)}
${#strings.concatReplaceNulls(str)}

/*
 * Random
 */
${#strings.randomAlphanumeric(count)}

三、使用Thymeleaf布局

Spring Boot 2.0 將布局單獨(dú)提取了出來(lái),需要單獨(dú)引入依賴:thymeleaf-layout-dialect。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

定義代碼片段

<footer th:fragment="copy"> 
&copy; 2019
</footer>

在頁(yè)面任何地方引入:

<body>
    <div th:insert="layout/copyright :: copyright"></div>
    <div th:replace="layout/copyright :: copyright"></div>
</body>

th:insert 和 th:replace 區(qū)別,insert 只是加載,replace 是替換。Thymeleaf 3.0 推薦使用 th:insert 替換 2.0 的 th:replace。

返回的 HTML 如下:

<body> 
   <div> &copy; 2019 </div> 
  <footer>&copy; 2019 </footer> 
</body>

下面是一個(gè)常用的后臺(tái)頁(yè)面布局,將整個(gè)頁(yè)面分為頭部,尾部、菜單欄、隱藏欄,點(diǎn)擊菜單只改變 content 區(qū)域的頁(yè)面

<body class="layout-fixed">
  <div th:fragment="navbar"  class="wrapper"  role="navigation">
	<div th:replace="fragments/header :: header">Header</div>
	<div th:replace="fragments/left :: left">left</div>
	<div th:replace="fragments/sidebar :: sidebar">sidebar</div>
	<div layout:fragment="content" id="content" ></div>
	<div th:replace="fragments/footer :: footer">footer</div>
  </div>
</body>

任何頁(yè)面想使用這樣的布局值只需要替換中見(jiàn)的 content 模塊即可

<html xmlns:th="http://www.thymeleaf.org" layout:decorator="layout">
 <body>
    <section layout:fragment="content">
  ...

也可以在引用模版的時(shí)候傳參

<head th:include="layout :: htmlhead" th:with="title='Hello'"></head>

layout 是文件地址,如果有文件夾可以這樣寫fileName/layout:htmlhead,htmlhead 是指定義的代碼片段 如th:fragment="copy"

以上就是SpringBoot 中Thymeleaf如何使用,小編相信有部分知識(shí)點(diǎn)可能是我們?nèi)粘9ぷ鲿?huì)見(jiàn)到或用到的。希望你能通過(guò)這篇文章學(xué)到更多知識(shí)。更多詳情敬請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

當(dāng)前題目:SpringBoot中Thymeleaf如何使用
轉(zhuǎn)載注明:http://aaarwkj.com/article20/ggpeco.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶體驗(yàn)、自適應(yīng)網(wǎng)站、全網(wǎng)營(yíng)銷推廣、動(dòng)態(tài)網(wǎng)站、軟件開發(fā)、網(wǎng)站維護(hù)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)
日本高清不卡在线一区二区| 国产产品在线免费看91| 午夜免费成人在线视频| 国产国产乱老熟视频网站| 国产成人国产三级国产精品| 麻豆色视频在线观看免费| 欧美日韩在线亚洲二区综二| 国产精品va在线观看入口| 不卡二卡三卡四卡精品| 在线国产视频一区二区三区| 九九蜜桃视频香蕉视频| 久草尤物视频在线观看| 未满十八禁止免费在线观看| 蜜桃少妇人妻一区二区视频| 亚洲国产区男人的天堂| 久久精品无人区乱码一区二区| 欧美精品激情在线不卡| 国产亚洲欧美日韩激情在线 | 亚洲日本欧洲二区精品| 国产精品一区二区av麻豆| 色噜噜狠狠狠久久综合一区| 日韩看片一区二区三区高清| 久久综合亚洲鲁鲁五月天| 欧美特黄高清在线观看| 久久青草视频在线观看| 成人18禁h黄在线看免费| 亚洲av天堂免费在线观看| 国产自拍成人精品视频| 人妻少妇偷人精品免费看| 亚洲一区二区三区 码| 日本理伦片一区二区| 日韩一区不卡在线观看| 91国产视频在线观看免费 | 国产一区二区主播不卡| 性色av人妻中文一区二区| 亚洲伊人成综合人影院| 国产精品亚洲欧美在线| 日韩欧美一区二区在线播放| 欧美午夜精品一二三区| 91午夜精品在线观看| 亚洲成人精品久久久|