當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot中Thymeleaf的初步使用
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot中Thymeleaf的初步使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
目錄
?
?
理論
演示
?
理論
使用TemplateEngine是Spring Boot中推薦的,他的作用是:
把模板(如html界面)和數據匹配好,然后輸出,發給用戶。
而不是傳統的使用jsp進行操作
?
模版和數據交給引擎,最后給出一個界面給用戶。
Thymeleaf語法簡單,功能強大;
xmlns:th="http://www.thymeleaf.org"通過這個能在寫HTML中獲取Thymeleaf的提示
?
演示
運行截圖如下:
程序結構如下:
源碼如下:
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";}//查出一些數據,顯示到頁面上@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的區別 --><div th:text="${HowAreYou}"></div><div th:utext="${HowAreYou}"></div><br><br><!-- 遍歷標簽 --><h4 th:text="${user}" th:each="user:${users}"></h4><br><h4><span th:each="user:${users}">[[${user}]]</span></h4><!-- 結束 --><h1>Hello</h1></body> </html>?
總結
以上是生活随笔為你收集整理的Spring Boot中Thymeleaf的初步使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OllyDbg笔记-对标志寄存器中ZF的
- 下一篇: Java笔记-使用RabbitMQ的Ja