spring boot 整合thyemleaf基本使用(一)
阿新 • • 發佈:2018-11-11
thyemleaf基本使用 :首先引入依賴thyemleaf
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
個人建議直接用IDEA在建立專案的時候選上對應的選項。
在Controller層:
@Controller public class DemoController { @RequestMapping("/show") public String showInfo(Model model){ model.addAttribute("msg","thymeleaf的第一個案例"); return "index"; } } HTML檔案:
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.w3.org/1999/xhtml"> <head> <meta charset="UTF-8"> <title>thymeleaf</title> </head> <body> <span th:text="hello"></span> <hr/> <span th:text="${msg}"></span> </body> </html>
這裡檔案的存放位置是在如圖這個資料夾下,這個資料夾在建立專案的時候會自動生成,如沒有請手動建立。
在h't'm'l檔案裡
<span th:text="hello"></span> 直接把hello顯示在網頁中
<span th:text="${msg}"></span> 顯示msg的值,這裡在Controller層中有個 model.addAttribute("msg","thymeleaf的第一個案例");
這是一個鍵值對的形式,在頁面顯示(thymeleaf的第一個案例)
關於HTML檔案的檔案位置識別問題,系統會自動預設。如果沒有預設,就自己在
properties中加
spring.thymeleaf.prefix=classpath:/templates/ spring.thymeleaf.suffix=.html spring.thymeleaf.mode=HTML5 spring.thymeleaf.encoding=UTF-8
歡迎致信[email protected] 可以一起討論相關知識。