4
4
import org .springframework .context .annotation .ComponentScan ;
5
5
import org .springframework .context .annotation .Configuration ;
6
6
import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
7
+ import org .springframework .web .servlet .config .annotation .ResourceHandlerRegistry ;
8
+ import org .springframework .web .servlet .config .annotation .WebMvcConfigurerAdapter ;
7
9
import org .springframework .web .servlet .view .InternalResourceViewResolver ;
8
10
import org .springframework .web .servlet .view .JstlView ;
9
11
10
12
/**
11
13
* MVC 配置类。这里我们配置了一个jsp的ViewResolver,用来映射路径和实际页面的位置,
12
- * 其中@EnableWebMvc注解会开启一些默认的配置,如一些ViewResolver或者MessageConverter
14
+ * 其中@EnableWebMvc注解会开启一些默认的配置,如一些ViewResolver或者MessageConverter。
15
+ *
16
+ * Spring MVC的定制配置需要我们的配置类继承WebMvcConfigurerAdapter类(Adapter适配器),
17
+ * 并在此类加上@EnableWebMvc注解,来开启对Spring MVC的配置支持,这样我们就可以重写这个类的方法,
18
+ * 来完成我们的配置。如果不加@EnableWebMvc注解,重写这些方法也无效。
13
19
* @author yuhao.wang
14
20
* @Date 2017年3月29日 下午3:41:20
15
21
*/
16
22
@ Configuration
17
- @ EnableWebMvc
23
+ @ EnableWebMvc // 开启对Spring MVC的支持,如果不加@EnableWebMvc注解,重写这些方法也无效。
18
24
@ ComponentScan ("com.chenfeng.xiaolyuh" )
19
- public class MvcConfig {
25
+ public class MvcConfig extends WebMvcConfigurerAdapter { // 重写WebMvcConfigurerAdapter类的方法可以对Spring MVC
20
26
21
27
@ Bean
22
28
public InternalResourceViewResolver viewResolver () {
@@ -26,4 +32,10 @@ public InternalResourceViewResolver viewResolver() {
26
32
viewResolver .setViewClass (JstlView .class );
27
33
return viewResolver ;
28
34
}
35
+
36
+ @ Override
37
+ public void addResourceHandlers (ResourceHandlerRegistry registry ) {
38
+ // addResourceLocations是指文件放置的目录,addResourceHandler是指对外暴露的地址
39
+ registry .addResourceHandler ("/assets/**" ).addResourceLocations ("classpath:/assets/" );
40
+ }
29
41
}
0 commit comments