springboot 项目 css,js静态文件404问题。

springboot 项目 css,js静态文件404问题。,第1张

springboot 项目 css,js静态文件404问题。

首先springboot 是默认路径没有/resource (这个应该都知道)和/static (主要问题出在这个上边)
两种解决方法:
1.再引用css,js的绝对路径上删除/static,这个方法工作量比较小可以使用。

2.实现WebMvcConfigurer接口,重写addResourceHandlers方法,这样html引用的时候就可以按正常路径/static/**写
再controller同级建立一个config包
添加类:

package com.example.demo.controller.config;

import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
 public class WebMvcConfigure implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    } }

注意: 一定不要忘记啊加@Configuration注释,他相当于controller类上加@Controller注释,作用一样,吧该类放到spring容器中

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5713141.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-18

发表评论

登录后才能评论

评论列表(0条)

保存