国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

SpringBoot 整合Thymeleaf教程及使用

這篇具有很好參考價(jià)值的文章主要介紹了SpringBoot 整合Thymeleaf教程及使用。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

Thymeleaf 是一款用于渲染 XML/XHTML/HTML5 內(nèi)容的模板引擎。它與 JSP,Velocity,F(xiàn)reeMaker 等模板引擎類似,也可以輕易地與 Spring MVC 等 Web 框架集成。與其它模板引擎相比,Thymeleaf 最大的特點(diǎn)是,即使不啟動(dòng) Web 應(yīng)用,也可以直接在瀏覽器中打開并正確顯示模板頁(yè)面 。

目錄

一、整合Thymeleaf

二、Thymeleaf 使用教程

三、Thymeleaf 實(shí)戰(zhàn)應(yīng)用


一、整合Thymeleaf

?1、引入Thymeleaf starter依賴

		<!-- thymeleaf 相關(guān)依賴 -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>

2、在 application.properties 添加 Thymeleaf 相關(guān)配置

server.port= 8092

#關(guān)閉 Thymeleaf 的緩存開發(fā)過(guò)程中無(wú)需重啟
spring.thymeleaf.cache = false
#設(shè)置thymeleaf頁(yè)面的編碼
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5
#設(shè)置thymeleaf頁(yè)面的后綴
spring.thymeleaf.suffix=.html
#設(shè)置thymeleaf頁(yè)面的存儲(chǔ)路徑
spring.thymeleaf.prefix=classpath:/templates/

Thymeleaf默認(rèn)會(huì)開啟頁(yè)面緩存,提高頁(yè)面并發(fā)能力。但會(huì)導(dǎo)致我們修改頁(yè)面不會(huì)立即被展現(xiàn),因此我們關(guān)閉緩存:spring.thymeleaf.cache=false

修改完畢頁(yè)面,需要使用快捷鍵:Ctrl + Shift + F9來(lái)刷新工程。

3、編寫訪問(wèn) Thymeleaf 頁(yè)面 Controller

@Controller
@RequestMapping("/hello")
public class ThymeleafHelloWrodController {
    @RequestMapping({"/", "/index"})
    @ResponseBody
    public String index(Model model,String id) {
        model.addAttribute("msg", "welcome you!" + id);

        Map<String,String> user = new HashMap<>();
        user.put("uid","11111");
        user.put("umc","testmc");
        user.put("etel","13550125501");
        model.addAttribute("user", user);
        return "index"; //返回的是index.html的頁(yè)面 
    }

	@RequestMapping("/thymeleaf")
	public String helloThymeleaf(Model model){
		model.addAttribute("hello","hello Thymeleaf!");
		return "hello/index";
	}
}


 

后臺(tái)springmvc 使用 Model 傳入數(shù)據(jù) (包名:org.springframework.ui.Model)

Thymeleaf的主要作用是把model中的數(shù)據(jù)渲染到html中,因此其語(yǔ)法主要是如何解析model中的數(shù)據(jù)。

3、Controller類編寫(@RequestBody注解是必須加上的,將java對(duì)象轉(zhuǎn)為json格式的數(shù)據(jù)。如果出現(xiàn)頁(yè)面顯示不了又沒(méi)有報(bào)錯(cuò)可能就是Controller類沒(méi)有加@RequestBody)

返回的是Thymeleaf 模板對(duì)應(yīng)的index.html的頁(yè)面?

- public interface Model{} ? ? ? ? ? ? //是一個(gè)接口
- public class ModelMap extends LinkedhashMap<String,Object>{} ? ? ? ? ? ?//繼承了LinkedHashMap
- public class ExtendedModelMap extends MOdelMap implements Model{} ? ? ? //繼承了ModelMap又實(shí)現(xiàn)了Model接口
? - public class BindingAwareModelMap{} ?//這個(gè)類對(duì)應(yīng)的子類,就可以去實(shí)例化ModelMap也可以實(shí)例化Model
? ? - //因?yàn)镸odelMap繼承了LinkedHashMap所以說(shuō),BindingAwareModelMap也可以實(shí)例化Map集合
?

4、Thymeleaf 頁(yè)面代碼如下

src/main/resources/templates/index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="renderer" content="webkit">
    <title>首頁(yè)</title>
    <link rel="shortcut icon" th:href="@{/favicon.ico}"/>
    <link th:href="@{/static/css/bootstrap.min.css}" rel="stylesheet"/>
    <link th:href="@{/static/css/font-awesome.min.css}" rel="stylesheet"/>
</head>
<body class="fixed-sidebar full-height-layout gray-bg" style="overflow:hidden">
<div id="wrapper">
    <h1 th:text="${msg}"></h1>
    <p data-th-text="${msg}">test p</p>
    <p th:inline="text">p:[(${msg})]</p>
</div>
<h2>
    <p>Name: <span th:text="${user.uid}">Jack</span>.</p>
    <p>Age: <span th:text="${user.umc}">21</span>.</p>
    <p>friend: <span th:text="${user.etel}">Rose</span>.</p>
</h2>

<script th:src="@{/static/js/jquery.min.js}"></script>
<script th:src="@{/static/js/bootstrap.min.js}"></script>
<script>
</script>

</body>
</html>

5、訪問(wèn)頁(yè)面

http://localhost:8092/index

http://localhost:8092/index?id=test

二、Thymeleaf 使用教程

Thymeleaf 模板引擎具有以下特點(diǎn):
動(dòng)靜結(jié)合:Thymeleaf 既可以直接使用瀏覽器打開,查看頁(yè)面的靜態(tài)效果,也可以通過(guò) Web 應(yīng)用程序進(jìn)行訪問(wèn),查看動(dòng)態(tài)頁(yè)面效果。
開箱即用:Thymeleaf 提供了 Spring 標(biāo)準(zhǔn)方言以及一個(gè)與 SpringMVC 完美集成的可選模塊,可以快速的實(shí)現(xiàn)表單綁定、屬性編輯器、國(guó)際化等功能。
多方言支持:它提供了 Thymeleaf 標(biāo)準(zhǔn)和 Spring 標(biāo)準(zhǔn)兩種方言,可以直接套用模板實(shí)現(xiàn) JSTL、 OGNL 表達(dá)式;必要時(shí),開發(fā)人員也可以擴(kuò)展和創(chuàng)建自定義的方言。
與 SpringBoot 完美整合:SpringBoot 為 Thymeleaf 提供了的默認(rèn)配置,并且還為 Thymeleaf 設(shè)置了視圖解析器,因此 Thymeleaf 可以與 Spring Boot 完美整合。

1、在頁(yè)面的 html 標(biāo)簽中聲明名稱空間

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

在 html 標(biāo)簽中聲明此名稱空間,可避免編輯器出現(xiàn) html 驗(yàn)證錯(cuò)誤,但這一步并非必須進(jìn)行的,即使我們不聲明該命名空間,也不影響 Thymeleaf 的使用。

把html 的名稱空間,改成:xmlns:th="http://www.thymeleaf.org" 會(huì)有語(yǔ)法提示

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

Thymeleaf的主要作用是把model中的數(shù)據(jù)渲染到html中,因此其語(yǔ)法主要是如何解析model中的數(shù)據(jù)。通過(guò)${}來(lái)獲取model中的變量,注意這不是el表達(dá)式,而是ognl表達(dá)式,但是語(yǔ)法非常像。

Thymeleaf崇尚自然模板,意思就是模板是純正的html代碼,脫離模板引擎,在純靜態(tài)環(huán)境也可以直接運(yùn)行?,F(xiàn)在如果我們直接在html中編寫 ${}這樣的表達(dá)式,顯然在靜態(tài)環(huán)境下就會(huì)出錯(cuò),這不符合Thymeleaf的理念。

<h1>
    歡迎您:<span th:text="${user.name}">請(qǐng)登錄</span>
</h1>

靜態(tài)情況下,th指令不會(huì)被識(shí)別,而是顯示默認(rèn)值:請(qǐng)登錄?

但是瀏覽器也不會(huì)報(bào)錯(cuò),把它當(dāng)做一個(gè)普通屬性處理。這樣span的默認(rèn)值請(qǐng)登錄就會(huì)展現(xiàn)在頁(yè)面
如果是在Thymeleaf環(huán)境下,th指令就會(huì)被識(shí)別和解析,而th:text的含義就是替換所在標(biāo)簽中的文本內(nèi)容,于是user.name的值就替代了 span中默認(rèn)的請(qǐng)登錄

如果不支持這種th:的命名空間寫法,那么可以把th:text換成 data-th-text,Thymeleaf也可以兼容。

th:text指令出于安全考慮,會(huì)把表達(dá)式讀取到的值進(jìn)行處理,防止html的注入。

例如,<p>你好</p>將會(huì)被格式化輸出為$lt;p$gt;你好$lt;/p$lt;。

如果想要不進(jìn)行格式化輸出,而是要輸出原始內(nèi)容,則使用th:utext來(lái)代替.

?Thymeleaf中所有的表達(dá)式都需要寫在指令中,指令是HTML5中的自定義屬性,在Thymeleaf中所有指令都是以th:開頭。因?yàn)楸磉_(dá)式${user.name}是寫在自定義屬性中,因此在靜態(tài)環(huán)境下,表達(dá)式的內(nèi)容會(huì)被當(dāng)做是普通字符串,瀏覽器會(huì)自動(dòng)忽略這些指令,這樣就不會(huì)報(bào)錯(cuò)了!

<h2>
    <p>Name: <span th:text="${user.name}">Jack</span>.</p>
    <p>Age: <span th:text="${user.age}">21</span>.</p>
    <p>friend: <span th:text="${user.friend.name}">Rose</span>.</p>
</h2>

對(duì)象.屬性名方式?

${user.name} 可以寫作${user['name']}

<h2 th:object="${user}">
    <p>Name: <span th:text="*{name}">Jack</span>.</p>
    <p>Age: <span th:text="*{age}">21</span>.</p>
    <p>friend: <span th:text="*{friend.name}">Rose</span>.</p>
</h2>

?當(dāng)數(shù)據(jù)量比較多的時(shí)候,頻繁的寫user.就會(huì)非常麻煩。

首先在 h2上 用 th:object="${user}"獲取user的值,并且保存
然后,在h2內(nèi)部的任意元素上,可以通過(guò) *{屬性名}的方式,來(lái)獲取user中的屬性,這樣就省去了大量的user.前綴了

<h2 th:object="${user}">
    <p>FirstName: <span th:text="*{name.split(' ')[0]}">Jack</span>.</p>
    <p>LastName: <span th:text="*{name.split(' ')[1]}">Li</span>.</p>
</h2>

ognl表達(dá)式本身就支持方法調(diào)用?

?Thymeleaf中提供了一些內(nèi)置對(duì)象,并且在這些對(duì)象中提供了一些方法,方便我們來(lái)調(diào)用。獲取這些對(duì)象,需要使用#對(duì)象名來(lái)引用。

一些環(huán)境相關(guān)對(duì)象
對(duì)象 ? ?作用
#ctx ? ?獲取Thymeleaf自己的Context對(duì)象
#requset ? ?如果是web程序,可以獲取HttpServletRequest對(duì)象
#response ? ?如果是web程序,可以獲取HttpServletReponse對(duì)象
#session ? ?如果是web程序,可以獲取HttpSession對(duì)象
#servletContext ? ?如果是web程序,可以獲取HttpServletContext對(duì)象
Thymeleaf提供的全局對(duì)象:
對(duì)象 ? ?作用
#dates ? ?處理java.util.date的工具對(duì)象
#calendars ? ?處理java.util.calendar的工具對(duì)象
#numbers ? ?用來(lái)對(duì)數(shù)字格式化的方法
#strings ? ?用來(lái)處理字符串的方法
#bools ? ?用來(lái)判斷布爾值的方法
#arrays ? ?用來(lái)護(hù)理數(shù)組的方法
#lists ? ?用來(lái)處理List集合的方法
#sets ? ?用來(lái)處理set集合的方法
#maps ? ?用來(lái)處理map集合的方法

?java代碼:

@GetMapping("show3")
public String show3(Model model){
    model.addAttribute("today", new Date());
    return "show3";
}

頁(yè)面代碼:?

<p>
  今天是: <span th:text="${#dates.format(today,'yyyy-MM-dd')}">2018-04-25</span>
</p>

${#strings.equals('編程幫',name)}

${#session.getAttribute('map')}
${session.map}

字面值:

基本類型如:字符串、數(shù)值、布爾等,并不希望被Thymeleaf解析為變量,這個(gè)時(shí)候稱為字面值

字符串字面值:th:text="'thymeleaf'" 使用一對(duì)'引用的內(nèi)容

數(shù)字字面值:th:text="2018" 數(shù)字不需要任何特殊語(yǔ)法, 寫的什么就是什么,而且可以直接進(jìn)行算術(shù)運(yùn)算

布爾字面值:th:if="true"

空字面值:<p th:text="${user == null}"></p>

<p>
? 你正在觀看 <span th:text="'thymeleaf'">template</span> 的字符串常量值.
</p>

拼接
我們經(jīng)常會(huì)用到普通字符串與表達(dá)式拼接的情況:

<span th:text="'歡迎您:' + ${user.name} + '!'"></span>

?字符串字面值需要用'',拼接起來(lái)非常麻煩,Thymeleaf對(duì)此進(jìn)行了簡(jiǎn)化,使用一對(duì)|即可:

<span th:text="|歡迎您:${user.name}|"></span>

運(yùn)算

<span th:text="${user.age}"></span>
<span th:text="${user.age}%2 == 0"></span>

支持的算術(shù)運(yùn)算符:+ - * / %

支持的比較運(yùn)算:>, <, >= and <=

但是>, <不能直接使用,因?yàn)閤ml會(huì)解析為標(biāo)簽,要使用別名。

注意 == and !=不僅可以比較數(shù)值,類似于equals的功能。

可以使用的別名:gt (>), lt (<), ge (>=), le (<=), not (!). Also eq (==), neq/ne (!=).

三元運(yùn)算:<span th:text="${user.sex} ? '男':'女'"></span>

默認(rèn)值:?<span th:text="${user.name} ?: '二狗'"></span>?( ?:之間沒(méi)有空格 )

${}內(nèi)部的是通過(guò)OGNL表達(dá)式引擎解析的,外部的才是通過(guò)Thymeleaf的引擎解析,因此運(yùn)算符盡量放在${}外進(jìn)行。?

設(shè)置屬性值

在 Thymeleaf 模板文件中,你可以使用th:*(或者使用th:attr屬性)來(lái)設(shè)置任意的 HTML5 標(biāo)簽屬性的值。不僅如此,你還可以th:*-*來(lái)同時(shí)為多個(gè)不同的標(biāo)簽屬性設(shè)置相同的一個(gè)值,甚至你可以使用th:attrappendth:attrprepend來(lái)追加新的值到現(xiàn)有的標(biāo)簽屬性值中。

th:attr 這種方式是不被推薦的

<!-- <div item-id="1001">Welcome to BeiJing!</div> -->
<div th:item-id="${user.id}">Welcome to BeiJing!</div>

<img src="logo.png" th:alt-title="LOGO圖片">

th:*? 中的*可以是 HTML5 支持的任意屬性名稱,甚至這些屬性名稱可以是自定義的

th:*-*?如果想要同時(shí)為標(biāo)簽的多個(gè)不同屬性設(shè)置相同的一個(gè)值,可以使用th:*-*的語(yǔ)法:

th:attrappend & th:attrprepend? 可以將表達(dá)式的結(jié)果分別追加到指定的屬性值之后和之前。

<!-- <button class="btn enable">購(gòu)買</button> -->
<button class="btn" th:attrappend="class=${outOfStock} ? ' enable' : ' disable'">購(gòu)買</button>
<!-- <button class="enable btn">購(gòu)買</button> -->
<button class="btn" th:attrprepend="class=${outOfStock} ? 'enable ' : 'disable '">購(gòu)買</button>

還有兩個(gè)常用的具體附加屬性th:classappend="..."th:styleappend=""。它們分別用來(lái)代替th:attrappend="class=..."th:attrappend="style=..."

<!-- <button class="btn enable">購(gòu)買</button> -->
<button class="btn" th:classappend="${outOfStock} ? ' enable' : ' disable'">購(gòu)買</button>

?循環(huán)/IF/SWITCH

<tr th:each="user : ${users}">
    <td th:text="${user.name}">Onions</td>
    <td th:text="${user.age}">2.41</td>
</tr>

<tr th:each="user,stat : ${users}">
    <td th:text="${user.name}">Onions</td>
    <td th:text="${user.age}">2.41</td>
</tr>

?stat對(duì)象包含以下屬性:

index,從0開始的角標(biāo)
count,元素的個(gè)數(shù),從1開始
size,總元素個(gè)數(shù)
current,當(dāng)前遍歷到的元素
even/odd,返回是否為奇偶,boolean值
first/last,返回是否為第一或最后,boolean值

<div th:switch="${user.role}">
  <p th:case="'admin'">用戶是管理員</p>
  <p th:case="'manager'">用戶是經(jīng)理</p>
  <p th:case="*">用戶是別的玩意</p>
</div>

?JS模板

<script th:inline="javascript">
    const user = /*[[${user}]]*/ {};
    const age = /*[[${user.age}]]*/ 20;
    console.log(user);
    console.log(age)
</script>

在script標(biāo)簽中通過(guò)th:inline="javascript"來(lái)聲明這是要特殊處理的js腳本

語(yǔ)法結(jié)構(gòu):

const user = /*[[Thymeleaf表達(dá)式]]*/ "靜態(tài)環(huán)境下的默認(rèn)值";

因?yàn)門hymeleaf被注釋起來(lái),因此即便是靜態(tài)環(huán)境下, js代碼也不會(huì)報(bào)錯(cuò),而是采用表達(dá)式后面跟著的默認(rèn)值。

鏈接表達(dá)式?@{}

@{}是專門用來(lái)處理 URL 鏈接地址的。
不管是靜態(tài)資源的引用,還是 form 表單的請(qǐng)求,凡是鏈接都可以用鏈接表達(dá)式 (@{...})。

鏈接表達(dá)式的形式結(jié)構(gòu)如下:

無(wú)參請(qǐng)求:@{/xxx}
有參請(qǐng)求:@{/xxx(k1=v1,k2=v2)}

絕對(duì)地址:?

<!-- https://fanlychie.github.io -->
<p th:text="@{https://fanlychie.github.io}"></p>

頁(yè)面相對(duì)地址示例:

<!-- commons/base.html -->
<p th:text="@{commons/base.html}"></p>

上下文相對(duì)地址(相對(duì)于當(dāng)前的服務(wù))示例:

<!-- /css/mian.css -->
<p th:text="@{/css/mian.css}"></p>

服務(wù)器相對(duì)地址(相對(duì)于部署在同一個(gè)服務(wù)器中的不同服務(wù))示例:

<!-- /image/upload -->
<p th:text="@{~/image/upload}"></p>

參數(shù)使用示例:

<!-- /css/mian.css?v=1.0 -->
<p th:text="@{/css/mian.css(v=1.0)}"></p>
<!-- /user/order?username=fanlychie -->
<p th:text="@{/user/order(username=${session.user.name})}"></p>
<!-- /user/order?username=fanlychie&status=PAIED -->
<p th:text="@{/user/order(username=${session.user.name},status='PAIED')}"></p>
<!-- /user/fanlychie/info -->
<p th:text="@{/user/{username}/info(username=${session.user.name})}"></p>?

片段表達(dá)式

~{}可以用來(lái)引用一段公共的 HTML 代碼片段。

?在 Thymeleaf 模板文件中,你可以使用th:fragment屬性來(lái)定義一段公共的代碼片段,然后你可以通過(guò)使用th:insert、th:replace屬性來(lái)將這些公共的代碼片段引入到模板文件中來(lái)。

th:insert是直接將代碼片段插入到標(biāo)簽體內(nèi)

th:replace則是用代碼片段直接替換標(biāo)簽體內(nèi)容。

src/main/resources/templates/base.html,通過(guò)th:fragment屬性定義一段公共的代碼片段:

<div id="footer" th:fragment="footerFragment">&copy; 2017 fanlychie</div>

src/main/resources/templates/index.html,通過(guò)th:insert屬性引用一段公共的代碼片段:

<div th:insert="~{base :: footerFragment}"></div>
<div th:replace="~{base :: footerFragment}"></div>

(1)其中,~{}是可選的,我們可以去掉這層的包裹:

<div th:insert="base :: footerFragment"></div>

(2)若 index.html 與 base.html 不在同級(jí)目錄,如 templates/commons/base.html:

<div th:insert="~{commons/base :: footerFragment}"></div>

(3)使用th:fragment屬性定義代碼片段時(shí),你還可以聲明一組參數(shù):
<div th:fragment="crumbs(parent, child)">
    <i th:text="${parent}"></i> <i th:text="${child}"></i>
</div>
    
<!--
<i>用戶中心</i>
<i>我的訂單</i>
-->
<div th:insert="::crumbs('用戶中心', '我的訂單')"></div>

三、Thymeleaf 實(shí)戰(zhàn)應(yīng)用

1、input 、textarea 賦值

//JAVA
@RequestMapping(value = { "formData", "" })
public String list(HttpServletRequest request, ModelMap model) {
	Entity entity = xxxService.getEntity();
	model.addAttribute("entity", entity);
	return "test.html";
}
<!-- HTML -->
<input type="text" th:attr="name=${entity.name},required=${entity.propRequired}" placeholder="請(qǐng)輸入"/>
<textarea th:text="${entity.remarks=='null'?'':entity.remarks}" placeholder="請(qǐng)輸入"></textarea>

2.復(fù)選框 判斷選中

//JAVA
@RequestMapping(value = { "checkboxValue", "" })
public String list(HttpServletRequest request, ModelMap model) {
	Entity entity = xxxService.getEntity();
	model.addAttribute("entity", entity);
	return "test.html";
}
<input type="checkbox"  th:checked="${entity.prop1 eq '1'}"> aaa
<input type="checkbox"  th:checked="${entity.prop2 eq '1'}"> bbb

3.下拉框 動(dòng)態(tài)賦值 與 回顯

//JAVA
@RequestMapping(value = { "selectList", "" })
public String list(HttpServletRequest request, ModelMap model) {
	List<selectData> selectList = xxxService.getSelectList();
	int type = xxxService.getType();
	model.addAttribute("type", type);
	model.addAttribute("selectList", selectList);
	return "test.html";
}
  • 下拉框數(shù)據(jù)填充
<select data-placeholder="請(qǐng)選擇"  id="selectData" >
   <option value=" " selected>請(qǐng)選擇</option>
   <option th:each="data:${selectList}" th:value="${data.id}" th:text="${data.name}"> </option>
</select>
  • 下拉框數(shù)據(jù)填充,判斷,回顯數(shù)據(jù)
<select>
	<option value=""  th:selected="${type eq ''}">請(qǐng)選擇</option>
	<option value="1" th:selected="${type eq 1}">字符型</option>
	<option value="2" th:selected="${type eq 2}">整型</option>
	<option value="3" th:selected="${type eq 3}">日期</option>
</select>
  • js設(shè)置下拉框選中,回顯數(shù)據(jù)
<script type="text/javascript">
    $("#selectData").ready(function() {
        var value= "[[${type}]]";
        $("#selectData option[value= " + value + "]").prop("selected",true);
    });
</script>

4.循環(huán)遍歷

@RequestMapping(value = { "loopTest", "" })
public String list(HttpServletRequest request, ModelMap model) {
	List<Entity> dataList = xxxService.getEntityList();
	Map<String,List<Entity>> dataMap = xxxService.getEntityMap();
	model.addAttribute("dataList", dataList);
	model.addAttribute("dataMap", dataMap);
	return "test.html";
}
  • 遍歷 list 數(shù)據(jù):循環(huán)生成表格數(shù)據(jù)
<table>
 <head>
 	<tr>
 		<th>ID</th>
		<th>名稱</th>
		<th>類型</th>
		<th>時(shí)間</th>
		<th>是否可用</th>
 </head>
 <body>
 	<tr th:each="data : ${dataList}" >
 		<td th:text="${data.id}"></td>
        <td th:text="${data.name}"></td>
 		<td th:switch="${data.type}">
   			<span th:case="1">字符型</span>
   			<span th:case="2">整型</span>
   			<span th:case="3">日期</span>
        </td>
        <td th:text="${#dates.format(data.createDate, 'yyyy-MM-dd HH:mm:ss')}"></td>
        <td>
   			<span th:if="${data.usable} eq '1'">
				<i class="fa fa-check"></i>
			</span>
        </td>
	</tr>
 </body>
</table>
  • 遍歷map數(shù)據(jù)
<div th:each="dataEntry,dataStat: ${dataMap}" >
	<div> [[${dataEntry.key}]] </div>
	<div th:each="data: ${dataEntry.value}">
		<div>
			<dt>[[${data.id}]]: </dt>
			<dd>[[${data.name}]]</dd>
		</div>							
	</div>
</div>

5.超鏈接 url傳參

@RequestMapping(value = { "formData", "" })
public String list(HttpServletRequest request, ModelMap model) {
	Entity entity = xxxService.getEntity();
	model.addAttribute("entity", entity);
	return "test.html";
}
<a th:href="@{test/form(id=${entity.id},name=${entity.name})}">超鏈接1</a>
<a th:href="@{test/form?id='+${entity.id}}+'&name='+${entity.name}">超鏈接2</a>

springboot配合thymeleaf,調(diào)用接口不跳轉(zhuǎn)頁(yè)面只顯示文本

問(wèn)題一:thymeleaf不跳轉(zhuǎn)頁(yè)面,只顯示文本index

@RequestMapping("/excel")
@RestController
public class OperateExcelController {

    @GetMapping(value = "")
    public String index() {
        //使用@RestController不能直接返回return "index",否則不會(huì)跳轉(zhuǎn)頁(yè)面,只會(huì)再頁(yè)面顯示index文本而已
        return "index";
    }

@RestController注解相當(dāng)于@ResponseBody和@Controller合在一起的作用。在使用@RestController注解Controller時(shí),Controller中的方法無(wú)法返回jsp頁(yè)面,或者h(yuǎn)tml,配置的視圖解析器 InternalResourceViewResolver不起作用,返回的內(nèi)容就是Return 里的內(nèi)容。

包括在Mapping注解使用的同時(shí)使用@ResponseBody時(shí)也會(huì)出現(xiàn)同樣的問(wèn)題。

解決方案

解決辦法①:去除@ResponseBody或?qū)⒑蠷est的注解換成對(duì)應(yīng)的原始注解@Controller;

@RequestMapping("/excel")
@Controller
public class OperateExcelController {
    @GetMapping(value = "")
    public String index() {
        return "index";
    }

?解決辦法②:不通過(guò)String返回,通過(guò)ModelAndView對(duì)象返回,上述例子可將return語(yǔ)句換成下面的句子,在使用ModelAndView對(duì)象返回的時(shí)候,不需要考慮有沒(méi)有@ResponseBody類似的注解。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-740403.html

@RequestMapping("/excel")
@RestController
public class OperateExcelController {
    @GetMapping(value = "")
    public ModelAndView index() {
        return new ModelAndView("index");
    }

到了這里,關(guān)于SpringBoot 整合Thymeleaf教程及使用的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 【Springboot】SpringBoot基礎(chǔ)知識(shí)及整合Thymeleaf模板引擎

    【Springboot】SpringBoot基礎(chǔ)知識(shí)及整合Thymeleaf模板引擎

    ??博客x主頁(yè):己不由心王道長(zhǎng)??! ??文章說(shuō)明:spring?? ?系列專欄:spring ??本篇內(nèi)容:對(duì)SpringBoot進(jìn)行一個(gè)入門學(xué)習(xí)及對(duì)Thymeleaf模板引擎進(jìn)行整合(對(duì)所需知識(shí)點(diǎn)進(jìn)行選擇閱讀呀~)?? ??每日一語(yǔ):在人生的道路上,即使一切都失去了,只要一息尚存,你就沒(méi)有絲毫理

    2023年04月23日
    瀏覽(24)
  • Thymeleaf詳細(xì)教程(SpringBoot版)

    Thymeleaf詳細(xì)教程(SpringBoot版)

    目錄 一、簡(jiǎn)單介紹 二、引入依賴 三、創(chuàng)建頁(yè)面 3.1 關(guān)于靜態(tài)資源目錄相關(guān)(選看) 問(wèn)題1:SpringBoot靜態(tài)資源目錄在哪里? 問(wèn)題2:如何修改SpringBoot默認(rèn)的靜態(tài)資源路徑? 問(wèn)題3:如何給靜態(tài)資源添加訪問(wèn)前綴? 問(wèn)題4:添加全局前綴 3.2 創(chuàng)建文件模板(選看) 3.3?thymeleaf 簡(jiǎn)單

    2024年02月11日
    瀏覽(15)
  • springboot整合security,mybatisPlus,thymeleaf實(shí)現(xiàn)登錄認(rèn)證及用戶,菜單,角色權(quán)限管理

    springboot整合security,mybatisPlus,thymeleaf實(shí)現(xiàn)登錄認(rèn)證及用戶,菜單,角色權(quán)限管理

    本系統(tǒng)為springboot整合security,mybatisPlus,thymeleaf實(shí)現(xiàn)登錄認(rèn)證及用戶,菜單,角色權(quán)限管理。頁(yè)面為極簡(jiǎn)模式,沒(méi)有任何渲染。 源碼:https://gitee.com/qfp17393120407/spring-boot_thymeleaf 架構(gòu)截圖 此處以用戶表為例,其他表數(shù)據(jù)可在源碼獲取。 用戶表 共用屬性 共用屬性自動(dòng)填充配置

    2024年02月07日
    瀏覽(21)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解①

    SpringBoot自帶模板引擎Thymeleaf使用詳解①

    目錄 前言 一、SpringBoot靜態(tài)資源相關(guān)目錄 二、變量輸出 2.1 在templates目錄下創(chuàng)建視圖index.html 2.2 創(chuàng)建對(duì)應(yīng)的Controller 2.3 在視圖展示model中的值 三、操作字符串和時(shí)間 3.1 操作字符串 3.2 操作時(shí)間 ????????Thymeleaf是一款用于渲染XML/HTML5內(nèi)容的模板引擎,類似JSP。它可以輕易的

    2024年02月08日
    瀏覽(20)
  • SpringBoot自帶模板引擎Thymeleaf使用詳解②

    SpringBoot自帶模板引擎Thymeleaf使用詳解②

    目錄 一、條件判斷和迭代遍歷 1.1 條件判斷 2.2 迭代遍歷 二、獲取域中的數(shù)據(jù)和URL寫法 2.1 獲取域中的數(shù)據(jù) 2.2 URL寫法 三、相關(guān)配置 語(yǔ)法 作用 th:if 條件判斷 準(zhǔn)備數(shù)據(jù) model.addAttribute(\\\"sex\\\",\\\"男\(zhòng)\\"); 使用實(shí)例 div ??? span th:if=\\\"${sex}==\\\'女\\\'\\\"這是女生/span ??? span th:if=\\\"${sex}==\\\'男\(zhòng)\\'\\\"這是男

    2024年02月08日
    瀏覽(17)
  • springboot項(xiàng)目開發(fā),使用thymeleaf前端框架的簡(jiǎn)單案例

    springboot項(xiàng)目開發(fā),使用thymeleaf前端框架的簡(jiǎn)單案例

    springboot項(xiàng)目開發(fā),使用thymeleaf前端框架的簡(jiǎn)單案例!我們看一下,如何在springboot項(xiàng)目里面簡(jiǎn)單的構(gòu)建一個(gè)thymeleaf的前端頁(yè)面。來(lái)完成動(dòng)態(tài)數(shù)據(jù)的渲染效果。 第一步,我們?cè)谏弦恍」?jié),已經(jīng)提前預(yù)下載了對(duì)應(yīng)的組件了。 如圖,springboot的強(qiáng)大之處就在于,它有一套完整的版本依

    2024年01月25日
    瀏覽(22)
  • Springboot 使用thymeleaf 服務(wù)器無(wú)法加載resources中的靜態(tài)資源異常處理

    Springboot 使用thymeleaf 服務(wù)器無(wú)法加載resources中的靜態(tài)資源異常處理

    Springboot使用thymeleaf,并 連接遠(yuǎn)程數(shù)據(jù)庫(kù) 啟動(dòng)時(shí),無(wú)法加載resources中的靜態(tài)資源 瀏覽器報(bào)錯(cuò) 后端啟動(dòng)時(shí)報(bào)錯(cuò) 前端打開頁(yè)面時(shí)后端報(bào)錯(cuò) 打包編譯項(xiàng)目,顯示找不到j(luò)s、css、html等靜態(tài)資源,但本地路徑并沒(méi)有寫錯(cuò),于是我去找編譯文件,查看是不是靜態(tài)資源沒(méi)有編譯到,打開項(xiàng)

    2024年02月04日
    瀏覽(22)
  • SpringBoot Thymeleaf模板引擎

    SpringBoot Thymeleaf模板引擎

    前端交給我們的頁(yè)面,是 html 頁(yè)面。如果是我們以前開發(fā),我們需要把他們轉(zhuǎn)成jsp頁(yè)面,jsp好處就是當(dāng)我們查出一些數(shù)據(jù)轉(zhuǎn)發(fā)到JSP頁(yè)面以后,我們可以用jsp輕松實(shí)現(xiàn)數(shù)據(jù)的顯示,及交互等。 jsp支持非常強(qiáng)大的功能,包括能寫Java代碼,但是呢,我們現(xiàn)在的這種情況,SpringBoot這

    2024年02月13日
    瀏覽(56)
  • 【SpringBoot】| Thymeleaf 模板引擎

    【SpringBoot】| Thymeleaf 模板引擎

    目錄 Thymeleaf 模板引擎 1. 第一個(gè)例子 2. 表達(dá)式 ①標(biāo)準(zhǔn)變量表達(dá)式 ②選擇變量表達(dá)式(星號(hào)變量表達(dá)式) ③鏈接表達(dá)式(URL表達(dá)式) 3. Thymeleaf的屬性 ①th:action ②th:method ③th:href ④th:src ⑤th:text ⑥th:style ⑦th:each (重點(diǎn)) ⑧條件判斷 if-unless ⑨switch-case 判斷語(yǔ)句 ⑩th:inline內(nèi)聯(lián)

    2024年02月08日
    瀏覽(21)
  • SpringBoot+thymeleaf實(shí)戰(zhàn)遇到的問(wèn)題

    SpringBoot+thymeleaf實(shí)戰(zhàn)遇到的問(wèn)題

    目錄 一、控制臺(tái): 二、數(shù)據(jù)庫(kù)查詢異常: 三、前后端錯(cuò)誤校驗(yàn) ?編輯 四、在serviceImp中需要添加一個(gè)eq條件,表示和數(shù)據(jù)庫(kù)中的哪個(gè)字段進(jìn)行比較,否則會(huì)查出所有數(shù)據(jù),導(dǎo)致500 五、使用流轉(zhuǎn)換數(shù)據(jù)更簡(jiǎn)潔 六、重復(fù)報(bào)錯(cuò),多次遇見 七、Mybatis使用,常見錯(cuò)誤: 1.名字寫錯(cuò)了

    2024年01月19日
    瀏覽(11)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包