javascript
SpringBoot使用Thymeleaf模板
??版權聲明:本文為博主原創文章,轉載請注明出處
Thymeleaf模板簡介
Thymeleaf模板是一個現代化的服務端java模板引擎對于所有的web和獨立環境
Thymeleaf的主要目標是為你的開發工作流程帶來優雅自然的模板 ------ HTML能正確的顯示在瀏覽器中,并且也可以作為靜態原型工作,允許開發團隊加強協作
在有Spring框架的模塊,與您最喜愛的工具的集成為一體,并能插入自己的功能,Thymeleaf是理想的現代化的HTML5 Web開發JVM ------ 雖然它可以做的更多
摘自官網:http://www.thymeleaf.org/
也是SpringBoot推薦使用的模板
SpringBoot配置
1. application.yml配置(application.properties同樣)
spring:thymeleaf:prefix: classpath:/templates/ # 必須以/結尾,否則報錯找不到模板suffix: .htmlmode: HTML5encoding: UTF-8cache: false # 關閉緩存,即時刷新。上線后需改為trueservlet:content-type: text/html2. 實現Controller
package com.imooc.controller;import com.imooc.pojo.User; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping;@Controller @RequestMapping("th") public class ThymeleafController {@GetMapping("/index")public String index(ModelMap map) {map.addAttribute("name", "thymeleaf-imooc");return "thymeleaf/index";}@GetMapping("/center")public String center() {return "thymeleaf/center/center";}@GetMapping("/test")public String test() {User user = new User();return "";}}3. 創建Thymeleaf模板
3.1 index.html(位于 templates/thymeleaf/ 目錄下)
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title></title> </head> <body>Thymeleaf模板引擎<h1 th:text="${name}">hello world</h1> </body> </html>3.2 center.html(位于 templates/thymeleaf/center/ 目錄下)
<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title></title> </head> <body>Thymeleaf模板引擎<h1>center page</h1> </body> </html>4. 啟動SpringBoot項目,測試即可
?
參考:
https://www.imooc.com/video/16721
轉載于:https://www.cnblogs.com/jinjiyese153/p/8566130.html
總結
以上是生活随笔為你收集整理的SpringBoot使用Thymeleaf模板的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 3-13 装饰器
- 下一篇: JS设置CSS样式的集中方式