1. Configure DispatcherServlet
DispatcherServlet is the core of Spring MVC. Here the request will first touch the framework, which is responsible for routing the request to other components.
The DispatcherServlet is configured in the servlet container using Java instead of the web.xml file.
SpittrWebAppInitializer.java
1 package spittr.config; 2 import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer; 3 4 public class SpittrWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 5 6 @Override 7 protected Class<?>[] getRootConfigClasses() { 8 // TODO Auto-generated method stub 9 return new Class<?>[] { RootConfig.class }; 10 } 11 12 @Override 13 protected Class<?>[] getServletConfigClasses() { 14 // TODO Auto-generated method stub 15 return new Class<?>[] { WebConfig.class }; 16 } 17 18 @Override 19 protected String[] getServletMappings() { // 将DispatcherServlet映射到“/” 20 // TODO Auto-generated method stub 21 return new String[] { "/" }; 22 } 23 24 }
Any class that extends AbstractAnnotationConfigDispatcherServletInitializer will automatically configure the Dispatcher-Servlet and Spring application context, which will be located in the application's servlet context.
2. Spring MVC configuration class
WebConfig.java
1 package spittr.config; 2 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.WebMvcConfigurerAdapter; 10 import org.springframework.web.servlet.view.InternalResourceViewResolver; 11 12 @Configuration 13 @EnableWebMvc // Enable Spring MVC 14 @ComponentScan( " spitter.web " ) // Enable component scanning 15 public class WebConfig extends WebMvcConfigurerAdapter { 16 @Bean // Configure the JSP view resolver 17 public ViewResolver viewResolver() { 18 InternalResourceViewResolver resolver = new InternalResourceViewResolver(); 19 resolver.setPrefix( " /WEB-INF/view/ " ); 20 resolver.setSuffix(".jsp"); 21 resolver.setExposeContextBeansAsAttributes(true); 22 return resolver; 23 } 24 25 @Override // 配置静态资源处理 26 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 27 // TODO Auto-generated method stub 28 configurer.enable(); 29 } 30 31 }
The first thing to notice in the code above is that WebConfig is now annotated with @Component-Scan, so the spitter.web package will be scanned for components. Controllers written later will be annotated with @Controller, which makes them a candidate bean for component scanning. Therefore, there is no need to explicitly declare any controllers in the configuration class.
Then added a ViewResolver bean. More specifically, the Internal-ResourceViewResolver. The function is to find the JSP file. When searching, it will add a specific prefix and suffix to the view name.
Finally, the new WebConfig class also extends WebMvcConfigurerAdapter and overrides its configureDefaultServletHandling() method. By calling the enable() method of DefaultServlet-HandlerConfigurer, DispatcherServlet is required to forward requests for static resources to the default servlet in the servlet container, instead of using DispatcherServlet itself to handle such requests.
3.RootConfig.java
Because it mainly focuses on Web development, and the application context created by DispatcherServlet for Web-related configuration has been configured, the current RootConfig is relatively simple
1 package spittr.config; 2 3 import org.springframework.context.annotation.ComponentScan; 4 import org.springframework.context.annotation.ComponentScan.Filter; 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 = { "spitter" }, excludeFilters = { 11 @Filter(type = FilterType.ANNOTATION, value = EnableWebMvc.class) }) 12 public class RootConfig { 13 14 }