測試代碼
pom.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.2</version>
</parent>
<packaging>jar</packaging>
<groupId>com.kaven</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>springboot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
啟動類:
package com.kaven.springboot;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication application = new SpringApplication(SpringbootApplication.class);
application.run(args);
}
}
用于通路靜态資源的接口:
package com.kaven.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
@Controller
public class IndexController {
@GetMapping("/index")
public String index() {
return "index.html";
}
}
靜态資源如下圖所示:
都是類似如下所示的
HTML
代碼。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>ITKaven</title>
</head>
<body>
<h1>/META-INF/resources</h1>
</body>
</html>
靜态資源預設目錄
預設情況下,
Spring Boot
從類路徑中名為
/static
、
/public
、
/resources
、
/META-INF/resources
的目錄或
ServletContext
的根目錄提供靜态資源,使用
Spring MVC
中的
ResourceHttpRequestHandler
來進行處理,可以通過添加自己的
WebMvcConfigurer
并重寫
addResourceHandlers
方法來修改該配置。
在一個獨立的
Web
應用程式中,容器中的預設
servlet
也會被啟用,并充當後備服務,如果
Spring
對靜态資源請求不進行處理,則會從
ServletContext
的根目錄提供靜态資源。大多數情況下,這種情況不會發生(除非修改預設的
MVC
配置),因為
Spring
總是可以通過
DispatcherServlet
處理請求。
靜态資源優先順序
當預設目錄存在同名的靜态資源(比如
HTML
頁面)時,通路該同名靜态資源會存在優先順序,啟動應用,使用
Postman
通路
http://localhost:8080/index
。
可見
/META-INF/resources
檔案夾下的靜态資源最先被通路到,修改該檔案夾的名稱,重新啟動應用,使用
Postman
再通路
http://localhost:8080/index
。
執行同樣的操作。
可見靜态資源預設目錄的優先順序為:
-
/META-INF/resources
-
/resources
-
/static
-
/public
為了不影響後面的測試,将所有檔案夾修改成原來的名稱。
靜态資源請求路徑模式
預設情況下,靜态資源的請求路徑模式為
/**
(即所有請求路徑),可以使用
spring.mvc.static-path-pattern
屬性對其進行設定。
application.properties
:
spring.mvc.static-path-pattern=/static/**
修改控制器:
package com.kaven.springboot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping("/static")
public class IndexController {
@GetMapping("/index")
public String index() {
return "index.html";
}
}
spring.mvc.static-path-pattern=/static/**
表示當請求路徑比對
/static/**
時,才會處理該擷取靜态資源的請求,比如通路
http://127.0.0.1:8080/static/index
。
自定義靜态資源位置
可以使用
spring.web.resources.static-locations
屬性自定義靜态資源的位置(會将預設值覆寫),根
servlet
上下文路徑
"/"
也會自動添加到該位置集合。
application.properties
:
spring.mvc.static-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/html,classpath:/my
配置中自定義靜态資源位置的先後順序就是靜态資源的優先順序。
自定義靜态資源檔案夾
修改
pom.xml
:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/my-resources</directory>
</resource>
</resources>
</build>
my-resources
檔案夾下的配置檔案
application.properties
:
server.port=8085
spring.mvc.static-path-pattern=/static/**
spring.web.resources.static-locations=classpath:/html,classpath:/my
my-resources
檔案夾下的靜态資源。
啟動應用,日志如下圖所示,說明新的配置檔案起作用了。
使用
Postman
通路
http://127.0.0.1:8085/static/index
。
如果存在
resources
檔案夾,
Spring Boot
預設使用該檔案夾下的靜态資源,包括配置檔案。
啟動應用,日志如下圖所示,說明
resources
檔案夾下的配置檔案起作用了。
使用
Postman
通路
http://127.0.0.1:8085/static/index
,也是通路到
resources
檔案夾下的靜态資源。
是以需要修改
resources
檔案夾的名稱或者直接删除掉,如果自定義靜态資源檔案夾還是沒有起作用,就需要使用
Maven
執行
clean
操作,再重新啟動應用。