最近寫了一下springboot , 碰到了一個配置 html 的問題 , 專門 記錄一下
首先 說明 , 有兩種 訪問html 的方式
1.通過后台跳轉到 html 頁面
現在比較流行的開發模式就是 前后端分離, 在分離的情況下 , 就無法直接訪問到 html , 需要通過 后端來跳轉
(1.) 添加maven
<!-- 動態頁面 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
(2.) 配置 application.yml
spring:
thymeleaf:
prefix:
classpath: /templates # 訪問template下的html文件需要配置模板,映射
cache: false # 開發時關閉緩存,不然沒法看到實時頁面
(3) controller
@Controller @RequestMapping("/delete/") public class deleteController { @RequestMapping("wrong") public String index() { return "wrong"; } }
注意 :
訪問方法跳轉頁面 方法請求加/ 返回到某一個頁面不用.后綴名 並且類上的注解改為@controller 不是@rest Controller
通過訪問 localhost:8080/delete/wrong 就可以訪問到 templates下的wrong.html頁面.
2.直接訪問 html 頁面
配置applicaiton.yml
不用通過方法訪問頁面 spring: resources: static-locations: classpath:/static/, classpath:/templates/
就可以通過 localhost:8080/wrong.html 訪問
感謝 知知.