今天被問到重定向的問題,后續又引起了靜態資源路徑配置的問題,在這里做一個總結,當然,順便添加默認訪問index.html。
一:默認訪問
1.默認路徑
在springboot中靜態資源的映射文件是在resources目錄下的static文件夾。
2.訪問index.html
在項目中,如何訪問我們的index.html呢。
首先看程序結構。
然后啟動項目。
輸入localhost:8090就可以訪問了,這個是默認的,不需要具體的寫index.html
二:重定向
1.程序
1 package com.jun; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 @SpringBootApplication 12 @RestController 13 public class SpringBootApplicationTest { 14 public static void main(String[] args) { 15 System.out.print(11); 16 SpringApplication.run(SpringBootApplicationTest.class,args); 17 } 18 19 /** 20 * 測試重定向,與驗證restful 21 * @param response 22 * @throws IOException 23 */ 24 @GetMapping("/hello") 25 public void hello(HttpServletResponse response) throws IOException { 26 response.sendRedirect("http://127.0.0.1:8090/test/tt.html"); 27 } 28 }
2.知識點
使用response.sendRedirect("絕對路徑");
3.注意點
在寫絕對路徑的時候,可以省略static。
三:資源配置
1.默認資源路徑
1 Spring Boot默認的靜態資源文件配置: 2 3 private static final String[] CLASSPATH_RESOURCE_LOCATIONS = { 4 5 "classpath:/META-INF/resources/", "classpath:/resources/", 6 7 "classpath:/static/", "classpath:/public/" };
2.自定義路徑的方法
改變springboot項目靜態資源文件訪問目錄的方式有兩種:
一種是直接在配置文件中進行設置,
另一種是我們編寫一個配置類,繼承org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter【已經棄用】或者WebMvcConfigurationSupport
並重寫addResourceHandlers(ResourceHandlerRegistry registry)方法,其實addResourceHandlers(ResourceHandlerRegistry registry)方法就是個空方法。
3.配置方式
spring.mvc.static-path-pattern=/**
表示所有的訪問都經過靜態資源路徑;如果不設置,則不需要在絕對路徑中考慮這個配置,如果配置了值,則需要考慮配置的值。在設置之后,在路徑中不寫配置的值則會報錯,找不到路徑。
spring.resources.static-locations
在這里配置靜態資源路徑,前面說了這里的配置是覆蓋默認配置,所以需要將默認的也加上,否則static
、public
等這些路徑將不能被當作靜態資源路徑
4.配置方式的實踐
classpath下的所有文件都是可以被訪問到的。但是這種方式還是不建議的,使用默認的即可,不過實踐可以學習一下。
重新設置目錄結構:
配置:
1 server.port=8090 2 #spring.mvc.static-path-pattern=/** 3 spring.resources.static-locations=classpath:/
在這里,其實可以不設置spring.mvc.static-path-pattern。
程序:
@GetMapping("/hello") public void hello(HttpServletResponse response) throws IOException { // response.sendRedirect("http://127.0.0.1:8090/test/tt.html"); response.sendRedirect("http://127.0.0.1:8090/template/yy.html"); }
和:
1 @GetMapping("/hello") 2 public void hello(HttpServletResponse response) throws IOException { 3 // response.sendRedirect("http://127.0.0.1:8090/test/tt.html"); 4 // response.sendRedirect("http://127.0.0.1:8090/template/yy.html"); 5 response.sendRedirect("http://127.0.0.1:8090/static/test/tt.html"); 6 }
5.程序設置
1 package com.jun.config; 2 3 import org.springframework.context.annotation.Configuration; 4 import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; 5 import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; 6 7 /** 8 * 這里主要用於設置靜態資源的訪問路徑的測試 9 */ 10 @Configuration 11 public class MvcConfig extends WebMvcConfigurationSupport{ 12 @Override 13 protected void addResourceHandlers(ResourceHandlerRegistry registry) { 14 // 這里之所以多了一"/",是為了解決打war時訪問不到問題 15 registry.addResourceHandler("/**").addResourceLocations("/template","classpath:/template"); 16 } 17 }
6.訪問
上面的程序說明,只能訪問template路徑下的靜態資源。