003 spring boot访问静态资源与重定向

  今天被问到重定向的问题,后续又引起了静态资源路径配置的问题,在这里做一个总结,当然,顺便添加默认访问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在这里配置静态资源路径,前面说了这里的配置是覆盖默认配置,所以需要将默认的也加上,否则staticpublic等这些路径将不能被当作静态资源路径

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路径下的静态资源。

  

原文地址:https://www.cnblogs.com/juncaoit/p/11177978.html