溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

如何使用Java中的Thymeleaf類

發(fā)布時(shí)間:2022-02-28 10:23:40 來源:億速云 閱讀:112 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要講解了“如何使用Java中的Thymeleaf類”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“如何使用Java中的Thymeleaf類”吧!

說起Java的Thymeleaf類,相信很多的小伙伴們對(duì)此都非常的陌生。Thymeleaf類作為Java中一個(gè)xml、xhtml以及html5的模板引擎的類庫,可以作為mvc的web應(yīng)用的view層。

Java代碼

package com.zzx.controller;

import com.zzx.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Arrays;

/**
 * @date: 2021/04/25/ 10:07
 * @author: ZhengZiXuan
 * @description: 由于Spring Boot 不推薦我們使用.jsp,所以我們就使用html配合thymeleaf來進(jìn)行數(shù)據(jù)的傳輸
 * @title: Thymeleaf簡(jiǎn)單使用
 */
@Controller
@RequestMapping("/thyme")
public class ThymeleafController {

    @RequestMapping("data")
    public String ShowData(Model model){
        model.addAttribute("text","<a href='#'>點(diǎn)擊1</a>");
        model.addAttribute("utext","<a href='#'>點(diǎn)擊1</a>");
        model.addAttribute("value","input值");
        model.addAttribute("user",new User(1,"張三"));
        model.addAttribute("num",100);
        model.addAttribute("flag",true);
        model.addAttribute("list", Arrays.asList("Java","WEB","UI"));
        return "data";
    }
}

前端代碼

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"><!--此處需要配置thymeleaf,可以不配置,但是會(huì)爆紅,不會(huì)影響最終效果-->
<head>
    <meta charset="UTF-8">
    <title>thymeleaf的簡(jiǎn)單使用</title>
</head>
<body>

<!--取出后的值,填充到p標(biāo)簽中間,將字符串的標(biāo)簽解析字符串-->
<p th:text="${text}"></p><br/><hr>

<!--取出后的值,填充到p標(biāo)簽中間,utext會(huì)將字符串的標(biāo)簽解析為html標(biāo)簽-->
<p th:text="${utext}"></p><br/>

<!--th:value,相當(dāng)于是給原value屬性賦值-->
<input th:value="${value}"/><br/><hr/>

<!--thymeleaf支持屬性導(dǎo)航, 對(duì)象.屬性-->
id:<p th:text="${user.id}"></p><br>
name:<p th:text="${user.name}"></p><br>
<br><hr/>
<p th:text="${num}"></p>
<br/><hr/>

<!--th:if 判斷,如果判斷成功,該標(biāo)簽內(nèi)的內(nèi)容會(huì)展示,否則不展示-->
<p th:if="${flag}== true">
    看這里看這里
</p>
<hr>
<ol>
    <!--th:each 變量
        1. th:each 屬性在哪個(gè)標(biāo)簽,哪個(gè)標(biāo)簽循環(huán)出現(xiàn)
        2. th:each= "遍歷得到結(jié)果變量 :${key}"
        3. 在當(dāng)前標(biāo)簽,或者內(nèi)部標(biāo)簽就可以使用"遍歷得到結(jié)果變量"
    -->
    <li th:text="${str}" th:each="str : ${list}"></li>
</ol>
</body>
</html>

感謝各位的閱讀,以上就是“如何使用Java中的Thymeleaf類”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)如何使用Java中的Thymeleaf類這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI