- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- Java基礎之Thymeleaf的簡(jiǎn)單使用
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來(lái)進(jìn)行數據的傳輸 * @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標簽中間,將字符串的標簽解析字符串--> <p th:text="${text}"></p><br/><hr> <!--取出后的值,填充到p標簽中間,utext會(huì )將字符串的標簽解析為html標簽--> <p th:text="${utext}"></p><br/> <!--th:value,相當于是給原value屬性賦值--> <input th:value="${value}"/><br/><hr/> <!--thymeleaf支持屬性導航, 對象.屬性--> 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 判斷,如果判斷成功,該標簽內的內容會(huì )展示,否則不展示--> <p th:if="${flag}== true"> 看這里看這里 </p> <hr> <ol> <!--th:each 變量 1. th:each 屬性在哪個(gè)標簽,哪個(gè)標簽循環(huán)出現 2. th:each= "遍歷得到結果變量 :${key}" 3. 在當前標簽,或者內部標簽就可以使用"遍歷得到結果變量" --> <li th:text="${str}" th:each="str : ${list}"></li> </ol> </body> </html>
到此這篇關(guān)于Java基礎之Thymeleaf的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)Java Thymeleaf的使用內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站