springboot访问不到html中的引用的静态文件!

本文探讨了SpringBoot项目中HTML引用的静态文件无法访问的问题。通过调整context-path,从'/property'到'/',解决了访问障碍。配置类的使用确保了配置生效,对于./的含义有待进一步研究。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目访问不到html中引用的静态文件

05:45:50.955 [http-nio-8085-exec-4] WARN  org.springframework.web.servlet.PageNotFound - No mapping for GET /property/templates/system/js/jquery-3.5.1.min.js

在这里插入图片描述
参考:https://blog.csdn.net/qq_37281398/article/details/107905807
对于静态页面需要直接放行!


<resources mapping="/resources/**" location="/resources/" />
  <resources mapping="/images/**" location="/images/" />
  <resources mapping="/js/**" location="/js/" />

再次遇到这个问题,好像在application.xml中的配置是不生效的,加了配置类才生效

@Configuration
public class WebConfig implements WebMvcConfigurer {
    private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
            "classpath:/META-INF/resources/", "classpath:/resources/",
            "classpath:/static/", "classpath:/templates/", "classpath:/resources/templates/"};


    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**").addResourceLocations(
                    "classpath:/META-INF/resources/webjars/");
        }
        if (!registry.hasMappingForPattern("/**")) {
            registry.addResourceHandler("/**").addResourceLocations(
                    CLASSPATH_RESOURCE_LOCATIONS);
        }

    }
}

实际配置:问题解决:

  # 引擎模板配置
  thymeleaf:
    cache: false # 关闭缓存
    mode: LEGACYHTML5 # 去除htm5严格校验
    prefix: classpath:/templates/system/ # 指定 thymeleaf 模板路径
    encoding: UTF-8 # 指定字符集编码
    suffix: .html
  mvc:
#    static-path-pattern: /static/**  # js ,css 等静态文件路径
    static-path-pattern: /templates/**  # js ,css 等静态文件路径

其中html中的 ./ 代表的含义待考证:

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

修改context-path :/property

之后出现访问不了;
在这里插入图片描述

在这里插入图片描述

修改context-path: /

又可以了:
在这里插入图片描述

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值