Springboot 2.0.5版本访问不到静态资源

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/SIMBA1949/article/details/82712077

Springboot 2.0.5版本访问不到静态资源

问题如图

这里写图片描述

解决方案

package top.simba1949.config;

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

/**
 * @author [email protected]
 * @date 2018/9/15 10:52
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    /**
     * 添加静态资源文件,外部可以直接访问地址
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //需要配置1:----------- 需要告知系统,这是要被当成静态文件的!
        //第一个方法设置访问路径前缀,第二个方法设置资源路径
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        registry.addResourceHandler("/templates/**").addResourceLocations("classpath:/templates/");
    }

}

访问测试

这里写图片描述

猜你喜欢

转载自blog.csdn.net/SIMBA1949/article/details/82712077