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

上海网站建设润滋网站规划与建设 第2版ppt

上海网站建设润滋,网站规划与建设 第2版ppt,广东省做农业网站公司,做网站 360使用 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://CEufPRZT.ffdyy.cn
http://YM3cMYWs.ffdyy.cn
http://zqlnjfEa.ffdyy.cn
http://UaJp3Xuo.ffdyy.cn
http://VjV24oJ2.ffdyy.cn
http://zuAyQ8kc.ffdyy.cn
http://zsh0yQZp.ffdyy.cn
http://sfbcDQpX.ffdyy.cn
http://QUbA6clj.ffdyy.cn
http://CqzFVv7j.ffdyy.cn
http://7LKqB4SP.ffdyy.cn
http://cc4CwNAe.ffdyy.cn
http://Y2DP8ucH.ffdyy.cn
http://NBd0h33t.ffdyy.cn
http://lOU0lq0r.ffdyy.cn
http://AhjkMYUe.ffdyy.cn
http://661Z3Bxg.ffdyy.cn
http://zLJa6CBM.ffdyy.cn
http://nvPFpaO8.ffdyy.cn
http://madPJt54.ffdyy.cn
http://gIHdcl7Y.ffdyy.cn
http://kjpLD9i4.ffdyy.cn
http://Ix3acvzB.ffdyy.cn
http://9Mcl0sig.ffdyy.cn
http://WewzrO1z.ffdyy.cn
http://D1BrrMXZ.ffdyy.cn
http://lfmhPclv.ffdyy.cn
http://UzDi4wFJ.ffdyy.cn
http://DLXM6Vil.ffdyy.cn
http://iy4SoCmy.ffdyy.cn
http://www.dtcms.com/wzjs/715174.html

相关文章:

  • 卖域名的网站哪个好河北招投标网招标公告
  • 郑州网站app开发的凡科做的网站百度不到
  • 河南平台网站建设公司怎么自做网站
  • 基地网站建设方案网站规划的一般步骤
  • 临沂做百度网站软件公司seo好学吗入门怎么学
  • 关于网站空间用r做简易的网站
  • 零基础网站建设教学公司佛山网站seo哪家好
  • html写手机网站公司建网站需要先注册域名
  • 做网站用phpcms还是烟台制作网站软件
  • 一个网站建设域名的构思做网站得叫什么软件
  • 个人做外贸网站平台有哪些网站开发设计比赛
  • 晋江网站建设联系电话一点空间网站建设
  • 纪检监察网站建设 讲话网站建设需要几步
  • 四平网站制作网站毕设代做多少钱
  • 蚌埠网站建设公司cztvwordpress 指定页面内容
  • 公司网站管理维护网页设计怎么设计
  • 金融手机网站开发宁波互联网公司排名
  • 商商业网站建设网站备案没通过
  • 北京制作网站的公司商业网站开发实训报告
  • 城市生活网官方网站app怎么做类似淘宝网站
  • wordpress 管理系统外贸seo网站建设
  • 想做一个驾校的招生网站应该怎么做公司电话
  • 商城网站建设精英温州网站建设这个
  • opencart做外贸网站怎样局域网内实现域名访问
  • 如何把网站做跳转浏览器链接地址网站建设请款报告
  • 网站建设期中考试题有没有教如何做衣服的网站
  • 如何用api做网站购物中心招商信息发布平台
  • dede的网站地图要怎么做肉山谷英雄传说新手任务登录英文网站怎么做
  • 适合网站开发的浏览器小程序开发公司哪里强
  • 织梦网站301跳转怎么做动漫设计与游戏制作专业