- 开启@EnableWebMvc注解,若没有该注解,重写WebMvcConfigurerAdaper方法则无效。
- addResourceHandler指定对外暴露的访问路径,addResourceLocation指定文件放置目录。
配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author Kevin
* @description
* @date 2016/7/4
*/
@Configuration
@EnableWebMvc
@ComponentScan("ch04.web")
public class MyMvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
// addResourceHandler指定对外暴露的访问路径,addResourceLocation指定文件放置目录
registry.addResourceHandler("/assets").addResourceLocations("classpath:/assets/");
}
}