springboot学习随笔(二)自定义静态资源路径

有的时候我们会把静态资源static public等目录下,有的时候需要自定义

那么就需要修改默认路径了。

思路

1.获得webmvc的适配器

2.在适配器里注册自己的路径

代码案例:

import org.springframework.context.annotation.Configuration;
import org.springframework.util.ResourceUtils;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
public class WebStaticCSS extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // TODO Auto-generated method stubD
        registry.addResourceHandler("/templates/**").addResourceLocations(
                (ResourceUtils.CLASSPATH_URL_PREFIX + "/templates/"));
        registry.addResourceHandler("/static/**").addResourceLocations(
                ResourceUtils.CLASSPATH_URL_PREFIX + "/static/");
        super.addResourceHandlers(registry);
    }

}

webMvcConfigurerAdaper:webMvc适配器

addResourceHandler: 添加自定义的地址

addResourceLocations:添加映射地址

这样我们就完成了自定义

后续分析springboot自动配置

评论

暂无评论