File tree Expand file tree Collapse file tree 2 files changed +45
-1
lines changed
Chapter_05/Spittr/src/main/java/spittr Expand file tree Collapse file tree 2 files changed +45
-1
lines changed Original file line number Diff line number Diff line change 1
1
package spittr .config ;
2
2
3
+
4
+ import org .springframework .context .annotation .ComponentScan ;
5
+ import org .springframework .context .annotation .Configuration ;
6
+ import org .springframework .context .annotation .FilterType ;
7
+ import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
8
+
9
+ @ Configuration
10
+ @ ComponentScan (basePackages = {"spittr" }, // 扫描spittr包下,不扫描带有@EnableWebMvc注解的配置文件
11
+ excludeFilters = {
12
+ @ ComponentScan .Filter (type = FilterType .ANNOTATION , value = EnableWebMvc .class )
13
+ })
3
14
public class RootConfig {
4
15
}
Original file line number Diff line number Diff line change 1
1
package spittr .controller ;
2
2
3
- public class WebConfig {
3
+ import org .springframework .context .annotation .Bean ;
4
+ import org .springframework .context .annotation .ComponentScan ;
5
+ import org .springframework .context .annotation .Configuration ;
6
+ import org .springframework .web .servlet .ViewResolver ;
7
+ import org .springframework .web .servlet .config .annotation .DefaultServletHandlerConfigurer ;
8
+ import org .springframework .web .servlet .config .annotation .EnableWebMvc ;
9
+ import org .springframework .web .servlet .config .annotation .WebMvcConfigurer ;
10
+ import org .springframework .web .servlet .view .InternalResourceViewResolver ;
11
+
12
+ /**
13
+ * DispatcherServlet的配置声明
14
+ */
15
+
16
+ @ Configuration
17
+ @ EnableWebMvc // 启用SpringMVC
18
+ @ ComponentScan ("spittr.controller" ) // 启用组件扫描 范围是spittr.controller包下
19
+ public class WebConfig implements WebMvcConfigurer {
20
+ // 配置JSP视图解析器
21
+ @ Bean
22
+ public ViewResolver viewResolver () {
23
+ InternalResourceViewResolver resolver = new InternalResourceViewResolver ();
24
+ resolver .setPrefix ("*/WEB-INF/views/" );
25
+ resolver .setSuffix (".jsp" );
26
+ resolver .setExposeContextBeansAsAttributes (true );
27
+ return resolver ;
28
+ }
29
+
30
+ // 配置静态资源的处理
31
+ @ Override
32
+ public void configureDefaultServletHandling (DefaultServletHandlerConfigurer configurer ) {
33
+ // 通过调用DefaultServletHandlerConfigurer的enable()方法,我们要求DispatcherServlet将对静态资源的请求转发到Servlet容器中默认的Servlet上
34
+ // 而不是使用DispatcherServlet本身来处理此类请求
35
+ configurer .enable ();
36
+ }
4
37
}
You can’t perform that action at this time.
0 commit comments