日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot中Thymeleaf的初步使用

發(fā)布時(shí)間:2025/3/15 javascript 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring Boot中Thymeleaf的初步使用 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

目錄

?

?

理論

演示


?

理論

使用TemplateEngine是Spring Boot中推薦的,他的作用是:

把模板(如html界面)和數(shù)據(jù)匹配好,然后輸出,發(fā)給用戶。

而不是傳統(tǒng)的使用jsp進(jìn)行操作

?

模版和數(shù)據(jù)交給引擎,最后給出一個(gè)界面給用戶。

Thymeleaf語法簡單,功能強(qiáng)大;

xmlns:th="http://www.thymeleaf.org"

通過這個(gè)能在寫HTML中獲取Thymeleaf的提示

?

演示

運(yùn)行截圖如下:

程序結(jié)構(gòu)如下:

源碼如下:

HelloControler.java

package com.jar.demo.controller;import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody;import java.util.Arrays; import java.util.Map;@Controller public class HelloController {@ResponseBody@RequestMapping("/hello")public String hello(){return "Hello World";}//查出一些數(shù)據(jù),顯示到頁面上@RequestMapping("/success")public String success(Map<String, Object> map){map.put("hello", "你好");map.put("HowAreYou", "<h1>咋了</h1>");map.put("users", Arrays.asList("zhangsan", "lisi", "wangwu"));return "success";} }

JarApplication.java

package com.jar.demo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication public class JarApplication {public static void main(String[] args) {SpringApplication.run(JarApplication.class, args);}}

success.html

<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>成功</h1><div th:text="${hello}"></div><!-- 使用th任意屬性,替換原有屬性 --><div id="div01" class="myDiv" th:id="${hello}" th:class="${hello}" th:text="${hello}">歡迎</div><!-- th:text 和 th:utext的區(qū)別 --><div th:text="${HowAreYou}"></div><div th:utext="${HowAreYou}"></div><br><br><!-- 遍歷標(biāo)簽 --><h4 th:text="${user}" th:each="user:${users}"></h4><br><h4><span th:each="user:${users}">[[${user}]]</span></h4><!-- 結(jié)束 --><h1>Hello</h1></body> </html>

?

總結(jié)

以上是生活随笔為你收集整理的Spring Boot中Thymeleaf的初步使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。