SpringBoot整合Thymeleaf
1.創建Thymeleaf的入門專案
-
maven構建簡單專案
-
修改pom檔案添加thymeleaf的坐標
<!-- thymeleaf的坐標 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> -
創建存放視圖的目錄:src/main/resources/templates
templates:該目錄是安全的,意味著該目錄下的內容是不允許外界直接訪問的(不能通過url直接訪問),

2.Thymeleaf的基本使用
1.Thymeleaf的特點
Thymeleaf是通過他特定的語法對html的標記做渲染,
2.撰寫controller
/**
* Thymeleaf 入門案例
*/
@Controller
public class DemoController {
@RequestMapping("/show")
public String showInfo(Model model) {
model.addAttribute("msg","Thymeleaf 第一個案例");
return "index";
}
}
3.撰寫頁面
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf 入門</title>
</head>
<body>
<span th:text="Hello"></span>
<hr/>
<span th:text="${msg}"></span>
</body>
</html>
4.撰寫啟動器
5.運行
有可能會出現沒有html結束標記的例外:
我的專案的Springboot的版本是2.1.2所以沒有出現此例外
- 加上結束標記即可,讓html的標記按照嚴謹的語法撰寫,
- 修改匯入的Thymeleaf的jar包的版本到 3.0以上
<properties>
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
</properties>
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/65094.html
標籤:Java
