当前位置: 首页 > wzjs >正文

网站建设飠金手指排名十三青岛做网站推广

网站建设飠金手指排名十三,青岛做网站推广,安徽省政府集约化网站建设招标,介绍自己的做的网站使用 cursor 进行老项目更新为 springboot 的 web 项目,发生了奇怪的问题,就是 html 文件访问正常,但是静态文件就是 404 检查了各种配置,各种比较,各种调试,最后放弃时候,清理没用的配置文件&…

使用 cursor 进行老项目更新为 springboot 的 web 项目,发生了奇怪的问题,就是 html 文件访问正常,但是静态文件就是 404

检查了各种配置,各种比较,各种调试,最后放弃时候,清理没用的配置文件,发现了一个老的配置类

@Configuration
public class DateTimeConfig extends WebMvcConfigurationSupport {Logger logger = LoggerFactory.getLogger(DateTimeConfig.class);@Beanpublic FormattingConversionService mvcConversionService() {logger.info("mvcConversionService 执行了");DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(false);DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));dateTimeRegistrar.registerFormatters(conversionService);DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();dateRegistrar.setFormatter(new DateFormatter("yyyy-MM-dd"));dateRegistrar.registerFormatters(conversionService);return conversionService;}
}

WebMvcConfigurationSupport 替换为 WebMvcConfigurerAdapter 解决了问题,原因在于两者在 Spring MVC 中的 行为差异配置优先级。以下是详细分析:


1. 行为差异

特性WebMvcConfigurationSupportWebMvcConfigurerAdapter
作用完全覆盖 Spring MVC 默认配置扩展 Spring MVC 默认配置
影响范围全局配置,优先级最高局部配置,优先级较低
推荐使用场景需要完全自定义 MVC 配置仅需扩展默认配置

2. 问题根源

使用 WebMvcConfigurationSupport 的问题
  • 覆盖默认配置:继承 WebMvcConfigurationSupport完全禁用 Spring Boot 的自动配置(如静态资源处理、视图解析器等)。
  • 静态资源失效:默认的静态资源路径(classpath:/static/)会被覆盖,导致资源无法访问。
使用 WebMvcConfigurerAdapter 的优势
  • 扩展默认配置WebMvcConfigurerAdapter扩展 Spring MVC 配置,不会覆盖默认行为。
  • 保留自动配置:Spring Boot 的自动配置(如静态资源处理)仍然生效。

3. 解决方案

推荐使用 WebMvcConfigurer

WebMvcConfigurerAdapter 已被标记为 @Deprecated,建议直接实现 WebMvcConfigurer 接口:

@Configuration
public class WebConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/plugins/**").addResourceLocations("classpath:/static/plugins/").setCachePeriod(0);}
}
关键配置对比
配置项WebMvcConfigurationSupportWebMvcConfigurer
静态资源处理需手动配置所有资源路径仅需扩展默认配置
视图解析器需手动配置自动配置 + 扩展
拦截器需手动注册自动配置 + 扩展

4. 验证步骤

  1. 检查静态资源路径

    jar -tvf target/*.jar | grep "mdui.min.css"
    # 正确输出:
    # BOOT-INF/classes/static/plugins/mdui/css/mdui.min.css
    
  2. 访问资源URL

    curl -v http://localhost:8080/plugins/mdui/css/mdui.min.css
    # 预期响应:
    # HTTP/1.1 200
    # Content-Type: text/css
    
  3. 查看资源加载日志
    application.yml 中开启调试日志:

    logging:level:org.springframework.web: DEBUG
    

    正常日志

    DEBUG o.s.w.s.r.ResourceHttpRequestHandler - Resource found: class path resource [static/plugins/mdui/css/mdui.min.css]
    
  • 优化后的代码
@Configuration
public class DateTimeConfig implements WebMvcConfigurer {private static final Logger logger = LoggerFactory.getLogger(DateTimeConfig.class);@Overridepublic void addFormatters(FormatterRegistry registry) {logger.info("DateTimeConfig: 注册自定义日期时间格式化器");// 注册 Java 8 日期时间格式化器DateTimeFormatterRegistrar dateTimeRegistrar = new DateTimeFormatterRegistrar();dateTimeRegistrar.setDateFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd"));dateTimeRegistrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));dateTimeRegistrar.registerFormatters(registry);// 注册传统日期格式化器DateFormatterRegistrar dateRegistrar = new DateFormatterRegistrar();dateRegistrar.setFormatter(new DateFormatter("yyyy-MM-dd"));dateRegistrar.registerFormatters(registry);}
}

5. 总结

  • WebMvcConfigurationSupport:适合需要 完全自定义 MVC 配置的场景,但会 覆盖默认行为
  • WebMvcConfigurer:适合 扩展默认配置,保留 Spring Boot 的自动配置能力。

通过替换为 WebMvcConfigurer,静态资源处理得以保留,问题自然解决。

http://www.dtcms.com/wzjs/193284.html

相关文章:

  • 长沙优化网站多少钱游戏推广引流软件
  • 厚街东莞网站推广收录情况
  • 郑州市网络科技有限公司搜索引擎优化排名品牌
  • h5模板网站模板一台电脑赚钱的门路
  • 疫情防控政策调整百度seo建议
  • 今天第四针最新消息杭州网站seo外包
  • 可以做交互的网站上海网络推广优化公司
  • 前端做网站需要的技能全网seo
  • 设计一个完整的静态网站宁波seo高级方法
  • 江苏网站建设公司排名百度集团股份有限公司
  • 专业开发网站建设友情链接交易
  • h5网站制作公司登录百度账号注册
  • 龙岗网站建设网站制作郑州优化公司有哪些
  • 宣传型网站bing搜索引擎国内版
  • 什么是建设企业网站网站免费制作平台
  • wordpress分类目录前缀连云港seo公司
  • cname解析对网站影响爱站数据
  • 网站做好了后怎么办电商网站建设哪家好
  • 网站分辨率做多大电脑培训零基础培训班
  • 天津网站建设培训班超级seo助手
  • 驻马店手机网站制作搜索引擎优化员简历
  • 沈阳网站建设公司怎么样产品推广平台排行榜
  • 在那个网站做驾校模拟题有没有帮忙推广的平台
  • 多品牌网站建设关键词首页排名优化
  • 杭州市拱墅区建设局网站东莞seo技术
  • 海南做网站的淘宝seo是什么
  • 三河市疫情最新情况优化推广网站怎么做最好
  • 临沂网站推广推广方式怎么写
  • 发帖推广平台朝阳seo排名优化培训
  • 浅析小型企业网站的建设东莞网站推广排名