书单推荐:成为Java顶级程序员架构师 ,这20来本(高薪)必看点击获取
SpringBoot访问静态资源目录,通过继承WebMvcConfigurerAdapter,实现WebMvcConfigurer,修改pom都可以实现。
创建一个StaticConfig 继承 WebMvcConfigurerAdapter
package com.huifer.blog.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* 描述:
* 静态文件配置
* @author huifer
* @date 2019-01-01
*/
@Configuration
public class StaticConfig extends WebMvcConfigurerAdapter {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/static/fonts/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
super.addResourceHandlers(registry);
}
}
创建一个StaticConfig 实现 WebMvcConfigurer
package com.huifer.blog.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 描述:
* 静态文件配置
* @author huifer
* @date 2019-01-01
*/
@Configuration
public class StaticConfig implements WebMvcConfigurer {
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/js/**").addResourceLocations("classpath:/static/js/");
registry.addResourceHandler("/css/**").addResourceLocations("classpath:/static/css/");
registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:/static/fonts/");
registry.addResourceHandler("/images/**").addResourceLocations("classpath:/static/images/");
// super.addResourceHandlers(registry);
}
}
修改pom 文件
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
以上三种方案都可以访问到 static目录
转载请注明:谷谷点程序 » SpringBoot访问静态资源:继承WebMvcConfigurerAdapter,实现WebMvcConfigurer