1目錄講解
src/main/java:存放代碼
src/main/resources
static:存放靜態文件,比如:css js image (訪問方式:http://localhost:8080/js/main.js)
templates:存放靜態頁面 jsp ,html,tpl
config : 存放配置文件,例如,application.properties
resources:存放腳本
2 pom.xml中引入依賴 Thymeleaf
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3文件的加載順序,先加載靜態文件
springboot會默認從 META/recources > recources>staitic>public 里面是否存在相應的資源,如果有則直接返回,如果一直找不到,就會報錯
可以自己在staic 下面多建幾個文件,文件加中新建test.js。里面寫一個測試js console.info("xxx") ,
然后run application ,
通過地址訪問 例如:http://localhost:8080/test.js
如果是訪問templates下面的index.html,還是需要通過controller進行映射index.html
上例子:
3.1 在templates下面新建一個index.html的頁面
3.2在pom.xml中添加模板依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
3.3在controller文件夾下添加FileController
3.4 運行application,訪問地址:http://localhost:8080//api/v1/index
請注意:controller中應該注釋掉有相對路徑的方法,否則無法訪問到對應的 圖片
4默認配置
(1)spring官網的默認配置
https://docs.spring.io/springboot/docs/current/reference/html/boot-features-developing-web-application.html#boot-features-spring-mvc-static-content
(2)在src/amin/resources中新建一個test文件夾,在里面有一個test.js,正常的localhost:8080/test/test.js是無法訪問的,需要在src/amin/resourcesxia 新建一個application.properties,里面復制
spring.resources.static-location=classpath:/META-INF/resources/,classpath:/recources/,classpath:/static/,classpath:/public/,classpath:/test/
然后重新啟動application,使用localhost:8080/test/test.js才能訪問到test.js