SpringBoot靜態資源路徑配置及主頁顯示
靜態資源路徑
靜態資源支持放在以下路徑中,訪問優先級從上到下:
classpath:/META-INF/resources/classpath:/resources/classpath:/static/ # 默認路徑classpath:/public/
其中 classpath 為 src/main/resources 目錄。
請求地址為:http://localhost:8080/xx.js
首頁
文件位置:
classpath:/static/favicon.icoclasspath:/templates/index.html
導入 thymeleaf 模板引擎依賴:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring5</artifactId> </dependency> <dependency> <groupId>org.thymeleaf.extras</groupId> <artifactId>thymeleaf-extras-java8time</artifactId> </dependency></dependencies>
定義請求控制器:
@Controllerpublic class IndexController { @RequestMapping({'/', '/index.html'}) public String index(Model model){ model.addAttribute('msg', 'Hello, Thymeleaf!'); return 'index'; }}
加入模板內容顯示首頁:
<!DOCTYPE html><html lang='en' xmlns:th='http://www.thymeleaf.org'><head> <meta charset='UTF-8'> <title>index page</title></head><body><h1>首頁</h1><div th:text='${msg}'></div></body></html>
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章:
