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

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

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

目錄

一、條件判斷和迭代遍歷

1.1 條件判斷

2.2 迭代遍歷

二、獲取域中的數(shù)據(jù)和URL寫法

2.1 獲取域中的數(shù)據(jù)

2.2 URL寫法

三、相關(guān)配置


一、條件判斷和迭代遍歷

1.1 條件判斷

語(yǔ)法 作用
th:if 條件判斷

準(zhǔn)備數(shù)據(jù)

model.addAttribute("sex","男");

使用實(shí)例

<div>
??? <span th:if="${sex}=='女'">這是女生</span>
??? <span th:if="${sex}=='男'">這是男生</span>
</div>

運(yùn)行結(jié)果:?

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃

當(dāng)然還有th:case也是相當(dāng)于Java中的switch

添加數(shù)據(jù)

model.addAttribute("id",2);

使用實(shí)例

<div th:switch="${id}">
??? <span th:case="1">id為1</span>
??? <span th:case="2">id為2</span>
??? <span th:case="3">id為3</span>
??? <span th:case="*">id為*</span>
</div>

運(yùn)行結(jié)果

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃

2.2 迭代遍歷

編寫實(shí)體類

package com.example.springbootdemo2.pojo;

public class User {
    private int id;
    private String name;
    private int age;

    public User() {
    }

    public User(int id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

準(zhǔn)備數(shù)據(jù)

// 添加List列表集合
User user1 = new User(1,"張三",100);
User user2 = new User(2,"李四",90);
User user3 = new User(3,"王五",60);
User user4 = new User(4,"老六",29);
List<User> users = new ArrayList<>();
users.add(user1);
users.add(user2);
users.add(user3);
users.add(user4);
model.addAttribute("users",users);?

在頁(yè)面中展示數(shù)據(jù)且配合狀態(tài)變量

thymeleaf將遍歷的狀態(tài)變量封裝到一個(gè)對(duì)象中,通過(guò)該對(duì)象的屬性可以獲取狀態(tài)變量:

狀態(tài)變量常用屬性
狀態(tài)變量 含義
index 當(dāng)前迭代器的索引,從0開(kāi)始
count 當(dāng)前迭代對(duì)象的計(jì)數(shù),從1開(kāi)始
size 被迭代對(duì)象的長(zhǎng)度
odd/even 布爾值,當(dāng)前循環(huán)是否是偶數(shù)/奇數(shù),從0開(kāi)始
first 布爾值,當(dāng)前循環(huán)的是否是第一條,如果是返回true,否則返回false
last 布爾值,當(dāng)前循環(huán)的是否是最后一條,如果是則返回true,否則返回false

使用實(shí)例

<table border="1">
??? <tr>
??????? <th>id</th>
??????? <th>姓名</th>
??????? <th>年齡</th>
??????? <th>當(dāng)前迭代器的索引,從0開(kāi)始</th>
??????? <th>當(dāng)前迭代對(duì)象的計(jì)數(shù),從1開(kāi)始</th>
??????? <th>被迭代對(duì)象的長(zhǎng)度</th>
??????? <th>布爾值,當(dāng)前循環(huán)是否是偶數(shù),從0開(kāi)始</th>
??????? <th>布爾值,當(dāng)前循環(huán)是否是奇數(shù),從0開(kāi)始</th>
??????? <th>布爾值,當(dāng)前循環(huán)的是否是第一條,如果是返回true,否則返回false</th>
??????? <th>布爾值,當(dāng)前循環(huán)的是否是最后一條,如果是則返回true,否則返回false</th>
??? </tr>
??? <tr th:each="user,status: ${users}">
??????? <td th:text="${user.getId()}"></td>
??????? <td th:text="${user.getName()}"></td>
??????? <td th:text="${user.getAge()}"></td>
??????? <td th:text="${status.index}"></td>
??????? <td th:text="${status.count}"></td>
??????? <td th:text="${status.size}"></td>
??????? <td th:text="${status.odd}"></td>
??????? <td th:text="${status.even}"></td>
??????? <td th:text="${status.first}"></td>
??????? <td th:text="${status.last}"></td>
??? </tr>
</table>

運(yùn)行結(jié)果:?

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃

遍歷Map

準(zhǔn)備數(shù)據(jù)

// 添加map集合數(shù)據(jù)
Map<String,User> userMap = new HashMap<>();
userMap.put("user1",user1);
userMap.put("user2",user2);
userMap.put("user3",user3);
userMap.put("user4",user4);
model.addAttribute("userMap",userMap);

使用實(shí)例?

<table>
??? <tr>
??????? <th>ID</th>
??????? <th>Name</th>
??????? <th>Age</th>
??????? <th>Key</th>
??? </tr>
??? <tr th:each="m : ${userMap}">
??????? <td th:text="${m.value.getId()}"></td>
??????? <td th:text="${m.value.getName()}"></td>
??????? <td th:text="${m.value.getAge()}"></td>
??????? <td th:text="${m.key}"></td>
??? </tr>
</table>

運(yùn)行結(jié)果:?

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃

二、獲取域中的數(shù)據(jù)和URL寫法

2.1 獲取域中的數(shù)據(jù)

thymeleaf也可以獲取request,session,application域中的數(shù)據(jù),方法如下:

準(zhǔn)備數(shù)據(jù)

// 往request域設(shè)置數(shù)據(jù)
req.setAttribute("req","request");
// 往response域設(shè)置數(shù)據(jù)
session.setAttribute("session","session");
// 往application域設(shè)置數(shù)據(jù)
session.getServletContext().setAttribute("app","application");

使用實(shí)例

request域獲取方式1: <span th:text="${#request.getAttribute('req')}"></span>
request域獲取方式2: <span th:text="${#httpServletRequest.getAttribute('req')}"></span>
<hr>
session域獲取方式1: <span th:text="${#session.getAttribute('session')}"></span>
session域獲取方式2: <span th:text="${#httpSession.getAttribute('session')}"></span>
<hr>
application域獲取方式1: <span th:text="${application.app}"></span>
application域獲取方式2: <span th:text="${#servletContext.getAttribute('app')}"></span>
<hr>

運(yùn)行結(jié)果:

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃

2.2 URL寫法

在Thymeleaf中路徑的寫法為 @{路徑},同樣也可以在路徑中添加參數(shù),使用RestFul樣式URL。

準(zhǔn)備數(shù)據(jù)

model.addAttribute("id",100);
model.addAttribute("name","lyl");

添加跳轉(zhuǎn)路徑

@GetMapping("/show2")
@ResponseBody
public String showPage2(String id,String name){
??? return id+":"+name;
}

// @RestFul風(fēng)格傳遞參數(shù)
@GetMapping("/show3/{id}/{name}")
@ResponseBody
public String showPage3(@PathVariable String id,@PathVariable String name){
??? return id + ":" + name;
}

使用實(shí)例

<a th:href="@{https://www.baidu.com}">百度</a>
<a th:href="@{show2?id=1&name='lyl'}">靜態(tài)參數(shù)一</a>
<a th:href="@{show2(id=1,name='hqx')}">靜態(tài)參數(shù)二</a>
<a th:href="@{'show2?id='+${id}+'&name='+${name}}">動(dòng)態(tài)參數(shù)一</a>
<a th:href="@{show2(id=${id},name=${name})}">動(dòng)態(tài)參數(shù)二</a>
<a th:href="@{show3/{id}/{name}(id=${id},name=${name})}">RestFul風(fēng)格傳遞參數(shù)</a><hr>

運(yùn)行結(jié)果

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃

三、相關(guān)配置

在Springboot配置文件中可以進(jìn)行Thymeleaf相關(guān)配置

thymeleaf相關(guān)配置項(xiàng)
配置項(xiàng) 含義
spring.thymeleaf.prefix 視圖前綴
spring.thymeleaf.suffix 視圖后綴
spring.thymeleaf.encoding 編碼格式
spring.thymeleaf.servlet.content-type 響應(yīng)類型
spring.thymeleaf.cache=false 頁(yè)面緩存,配置為false則不啟用頁(yè)面緩存,方便測(cè)試

SpringBoot自帶模板引擎Thymeleaf使用詳解②,SpringBoot,spring boot,后端,java,thymeleaf,原力計(jì)劃文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-715939.html

到了這里,關(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整合模板引擎Thymeleaf(4)

    SpringBoot整合模板引擎Thymeleaf(4)

    本文原創(chuàng)作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl 在之前的教程中,我們介紹了Thymeleaf的基礎(chǔ)知識(shí)。在此,以案例形式詳細(xì)介紹Thymeleaf的基本使用。 要點(diǎn)概述: 1、在static下創(chuàng)建css文件夾用于存放css文件 2、在static下創(chuàng)建img文件夾用于存放圖片文件 請(qǐng)?jiān)趐om.xml文

    2024年02月10日
    瀏覽(19)
  • 【SpringBoot學(xué)習(xí)筆記】04. Thymeleaf模板引擎

    【SpringBoot學(xué)習(xí)筆記】04. Thymeleaf模板引擎

    ?所有的html元素都可以被thymeleaf替換接管? th:元素名 templates下的只能通過(guò)Controller來(lái)跳轉(zhuǎn),templates前后端分離,需要模板引擎thymeleaf支持 ?? 模板引擎的作用就是我們來(lái)寫一個(gè)頁(yè)面模板,比如有些值呢,是動(dòng)態(tài)的,我們寫一些表達(dá)式。而這些值,從哪來(lái)呢,就是我們?cè)诤笈_(tái)封

    2024年02月13日
    瀏覽(21)
  • 【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)
  • 15 springboot項(xiàng)目——thymeleaf語(yǔ)法與關(guān)閉模板引擎

    15 springboot項(xiàng)目——thymeleaf語(yǔ)法與關(guān)閉模板引擎

    ? ? ? ? 在html文件中,有些是需要使用本地的css樣式,使用thymeleaf語(yǔ)法加載: ? ? ? ? 首先對(duì)head標(biāo)簽上面的html標(biāo)簽進(jìn)行更改: ? ? ? ? 其次,導(dǎo)入thymeleaf依賴: ? ? ? ? 接著,使用thymeleaf語(yǔ)法: ? ? ? ? 碰到href或者src后邊與靜態(tài)資源有關(guān)的的本地路徑要進(jìn)行修改,把要

    2024年02月14日
    瀏覽(18)
  • 【Spring Boot】Thymeleaf模板引擎 — 表達(dá)式的語(yǔ)法

    模板的主要作用是將后臺(tái)返回的數(shù)據(jù)渲染到HTML中。那么Thymeleaf是如何解析后臺(tái)數(shù)據(jù)的呢?接下來(lái)從變量、方法、條件判斷、循環(huán)、運(yùn)算(邏輯運(yùn)算、布爾運(yùn)算、比較運(yùn)算、條件運(yùn)算)方面學(xué)習(xí)Thymeleaf表達(dá)式支持的語(yǔ)法。 (1)文本賦值 賦值就是通過(guò)${}標(biāo)簽將后臺(tái)返回的數(shù)據(jù)替

    2024年02月14日
    瀏覽(24)
  • 前端模板引擎Thymeleaf的整合和使用

    目錄 一、添加依賴 1.1首先,在項(xiàng)目的構(gòu)建文件中(比如 Maven 或 Gradle)添加 Thymeleaf 的依賴。例如,對(duì)于 Maven 項(xiàng)目,在 pom.xml 文件中添加以下依賴 1.2保存并更新項(xiàng)目依賴 二、配置Thymeleaf 2.1模板位置配置 2.2模板緩存配置 2.3自定義標(biāo)簽配置 三、創(chuàng)建模板文件 3.1創(chuàng)建一個(gè)HTML文

    2024年04月27日
    瀏覽(31)
  • thymeleaf模板引擎

    thymeleaf模板引擎

    ThymeleafProperties 配置類 1.默認(rèn)編碼 2.前綴 3.后綴 相當(dāng)于視圖解析器? ? 這是學(xué)SpringBoot的必經(jīng)之路,非常重要?。。。ǔ悄闶菍W(xué)前端的) ? 只改了前端代碼點(diǎn)一下這個(gè)就可以刷新? ? 傳值過(guò)來(lái)了? th:text=\\\"${msg}\\\"爆紅,但是可以顯示,F(xiàn)ile-Settings-Editor-Inspection ?取消“Expression

    2024年02月14日
    瀏覽(19)
  • 使用 Velocity 模板引擎的 Spring Boot 應(yīng)用

    使用 Velocity 模板引擎的 Spring Boot 應(yīng)用

    模板引擎是構(gòu)建動(dòng)態(tài)內(nèi)容的重要工具,特別適用于生成HTML、郵件內(nèi)容、報(bào)告和其他文本文檔。Velocity是一個(gè)強(qiáng)大的模板引擎,它具有簡(jiǎn)單易用的語(yǔ)法和靈活性。本文將介紹如何在Spring Boot應(yīng)用中使用Velocity模板引擎,并提供示例代碼。 Velocity是一個(gè)用于生成文本輸出的模板引擎

    2024年02月07日
    瀏覽(52)
  • 基于Springboot+thymeleaf旅游景區(qū)管理系統(tǒng)——LW模板

    基于java的旅游管理系統(tǒng) 隨著我國(guó)經(jīng)濟(jì)的快速發(fā)展以及改革開(kāi)放政策的不斷完善,旅游已經(jīng)成為了人們假期放松旅游的主要方式之一。我國(guó)也越來(lái)越重視旅游業(yè)的發(fā)展,出臺(tái)了《關(guān)于促進(jìn)全域旅游發(fā)展的指導(dǎo)意見(jiàn)》、《“十四五”文化和旅游發(fā)展規(guī)劃》等政策予以扶持。旅游

    2024年02月11日
    瀏覽(22)
  • freemarker模板引擎詳解以及使用方法

    freemarker模板引擎詳解以及使用方法

    哈嘍!大家好,我是曠世奇才李先生 文章持續(xù)更新,可以微信搜索【小奇JAVA面試】第一時(shí)間閱讀,回復(fù)【資料】更有我為大家準(zhǔn)備的福利喲,回復(fù)【項(xiàng)目】獲取我為大家準(zhǔn)備的項(xiàng)目 相關(guān)閱讀 面試官:Zookeeper是什么,它有什么特性與使用場(chǎng)景? 面試官:Redis如何實(shí)現(xiàn)持久化的

    2024年02月09日
    瀏覽(29)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

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

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

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包