springboot静态资源解析失效处理

问题1:继承WebMvcConfigurationSupport导致默认配置失效

我们开发了一个基于SpringBoot的工具(starter封装),可以提供通用的功能和管理页面。为了让这些页面能够在不同的SpringBoot项目中使用,我们采用了一种叫做webjars的技术,将页面的静态资源(如JavaScript和CSS文件)存放在一个公共的地方。但是有些项目访问这些资源时可能会遇到404错误的问题,无法正常显示页面。

分析

首先springboot默认提供包括static webjars的资源解析的。这个是默认的mvc配置提供。因此检查出现404的项目的所有mvc配置是否有被调整。

发现该项目存在跨域配置. 是通过WebMvcConfigurationSupport继承实现

@Configuration
public class AppConfig extends WebMvcConfigurationSupport {

  /**
   * 统一处理跨域问题
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE", "PUT").allowCredentials(true)
            .maxAge(3600);
  }
}
  • 结论: 通过WebMvcConfigurationSupport继承实现会导致默认mvc配置失效。
  • 原因: 看如下源码
    在这里插入图片描述

解决

方式1:WebMvcConfigurationSupport补充静态资源【不推荐】

静态资源解析失效,因为是覆盖默认mvc配置,那么。直接补充就好

@Configuration
public class AppConfig extends WebMvcConfigurationSupport {

  /**
   * 统一处理跨域问题
   */
  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedOrigins("*").allowedMethods("GET", "POST", "DELETE", "PUT").allowCredentials(true)
            .maxAge(3600);
  }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问的问题
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

虽然能解决问题,WebMvcConfigurationSupport但是这种方式不太好,以后还有可能其他默认行为被覆盖,还得补。

方式2:改写mvc自定义配置实现【推荐】

实现WebMvcConfigurer接口,只会覆盖实现接口逻辑。springboot默认的mvc配置不会被改变

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
 


    /** 跨域支持*/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("POST", "GET", "PUT", "OPTIONS", "DELETE")
                .allowCredentials(true)
                .allowedHeaders("*")
                .maxAge(3600);
    }
}

总结

springboot项目中想要调整mvc配置.尽量通过实现WebMvcConfigurer 接口实现。直接继承WebMvcConfigurationSupport会,导致默认处理失效。

问题2: 前端页面访问static下资源404

Spring Boot 支持访问位于 static 目录下的静态资源,例如 CSS 和 JavaScript 文件。

默认情况下,Spring Boot 会将 static 目录作为静态资源的根目录。这意味着你可以将静态资源文件(如 CSS 和 JavaScript 文件)放置在 static 目录下的任何子目录中,并通过相对路径进行访问

后端资源目录

└── src
    └── main
        └── resources
            └── static
                ├── css
                │   └── styles.css
                └── js
                    └── script.js

前端访问

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" type="text/css" href="/css/styles.css">
    <script src="/js/script.js"></script>
</head>
<body>
    <!-- 页面内容 -->
</body>
</html>

注意“/css/styles.css” 前面不要再加static

问题3: springmvc访问webjars下资源404

背景: 开发一个starter服务兼容让springmvc项目使用,自定义WebMvcConfigurer 配置类可以设置静态资源访问

@Configuration
@Import({EasyLogWebAutoConfiguration.class})
public class EasyLogMvcConfig implements WebMvcConfigurer {

    /**等效于springmvc.xml 添加<mvc:default-servlet-handler/> //可访问前端静态资源*/
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

}

easy-log-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <description>easy-log Spring MVC Configuration</description>
    <bean class="com.zoe.easylog.web.mvc.EasyLogMvcConfig"></bean>
</beans>

接入项目导入配置文件即可

    <import resource="classpath*:easy-log-mvc.xml"/>

但是会出现有些springmvc生效有些项目会不生效。还是只能通过xml定义

easy-log-mvc.xml 补充

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <description>easy-log Spring MVC Configuration</description>
    <bean class="com.zoe.easylog.web.mvc.EasyLogMvcConfig"></bean>
    <!--开启静态资源访问-->
    <mvc:default-servlet-handler/>
</beans>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值