整合Thymeleaf引擎
本部分的學習涉及到了前端頁面與後端程序之間的交互,需要用到前端頁面,所以使用Thymeleaf引擎呈現前端頁面。
1.在pom.xml文件中添加依賴。
<!--Thymeleaf依賴--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
2.在com.example.mybatisdemo.controller包下,新建一個控制器DataCheckController。代碼如下:
package com.example.mybatisdemo.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.RestController; @Controller public class DataCheckController { @RequestMapping("/index") public String index(){ return "/index"; } }
3.創建HTML頁面
在resources下的templates下新建Html頁面index.html.內容如下:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h3>首頁</h3> </body> </html>
4.啓動測試
啓動項目後,打開瀏覽器,輸入以下URL,結果如下: