4.web工程的準備工作
阿新 • • 發佈:2021-06-17
1.靜態資源可以放置的位置:
優先順序resources》static》public
2.首頁的準備
直接放到public等資源包的下面然後命名為index
在template頁面下的檔案只能通過controller來訪問
3.使用模板引擎 匯入依賴 所有的東西寫在templates裡面
<dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency>
在html下匯入如下名稱空間,並且傳遞值
@Controller public class IndexController { @RequestMapping("/test")public String index(Model model){ model.addAttribute("msg","helloSpringBoot"); return "test"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--所有的html元素都可以被thymeleaf替換接管 th:元素名 這樣才能使用表示式--> <div th:text="${msg}"></div> </body> </html>
th的功能
轉義文字的處理以及遍歷
@Controller public class IndexController { @RequestMapping("/test") public String index(Model model){ model.addAttribute("msg","<h1>helloSpringBoot</h1>"); model.addAttribute("users", Arrays.asList("wu","yi")); return "test"; } }
<!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <!--所有的html元素都可以被thymeleaf替換接管 th:元素名 這樣才能使用表示式--> <div th:text="${msg}"></div> <!--處理轉義--> <div th:utext="${msg}"></div> <hr> <!--遍歷--> <h3 th:each="user:${users}" th:text="${user}"></h3> </body> </html>