SpringBoot的静态资源访问问题

一、默认的两个静态资源目录

  1. classpath:/public/
  2. classpath:/static/

这两个目录下的资源文件可直接通过文件名访问(不加public和static这一层目录)
在这里插入图片描述

二、默认的页面目录(该目录不能通过url直接获取,往往需要被模板引擎解析完写入到响应流中)

  1. classpath:/temlpates

三、静态资源存在,但是获取不到问题

查看项目运行后编译的classpath下的目录结构是否与源文件目录结构相同

在这里插入图片描述
对于maven项目来说,classpath目录就是target/classes目录。
如果编译后的目录结构与源文件目录结构不匹配,则可使用maven -package命令重新打包编译
在这里插入图片描述

四、Vue项目的打包问题

vue项目打包后的目录结构

在这里插入图片描述其中html和js中引入的资源路径都是以当前目录作为相对路径引入的,将来部署到服务器时,也是相对资源路径,例如访问index.html路径假设是 http://127.0.0.1:8080/views/index.html,那么js和css文件也应该从http://127.0.0.1:8080/views/开始,http://127.0.0.1:8080/views/css/xxx,http://127.0.0.1:8080/views/js/xxx。

此时只需将index.html,放到templates目录下,将其他静态资源放到static目录下即可。
在这里插入图片描述
为index.html 配置一个控制器

    @GetMapping("/")
    public String index(){
        return "index";
    }

五、拦截器问题

使用.excludePathPatterns() 方法放行资源无效,不知是不是springboot底层做了修改,暂时在拦截器的preHandler方法做了判断。

    {
        noAuthUrl.add("/");
        noAuthUrl.add("/sysuser/login");
        noAuthUrl.add("/sysuser/captcha");
        noAuthUrl.add("/error");
        noAuthUrl.add("/favicon.ico");
        noAuthUrl.add("/sysuser/captcha");
        noAuthUrl.add("/sysuser/captcha");
        noAuthUrl.add("/sysuser/captcha");
        noAuthUrl.add("/template_user_import.xlsx");

        staticUrl.add("/js/");
        staticUrl.add("/css/");
        staticUrl.add("/img/");
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestURI = request.getRequestURI();
        if (noAuthUrl.contains(requestURI)){
            return true;
        }
        //静态资源
        boolean anyMatch = staticUrl.stream().anyMatch(item -> requestURI.startsWith(item));
        if (anyMatch){
            return true;
        }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值