Spring 5.0 Cookbook
上QQ阅读APP看书,第一时间看更新

How to do it...

Perform the following to build a simple Spring MVC project using the JavaConfig specification:

  1. Create the JavaConfig root context through implementing the abstract class org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter. It has the @Configuration annotation and contains methods that can be overridden to manage validators, view resolvers, controllers, interceptors, and other MVC-specific components needed to be injected to the container. Let us name the implementation class SpringDispatcherConfig and place it inside the org.packt.starter.ioc.dispatcher package:
@Configuration 
public class SpringDispatcherConfig extends 
         WebMvcConfigurerAdapter{ } 
  1. Let us now configure the DispatcherServlet. The implementation of WebApplicationInitializer permits us to programmatically create, configure, and register the DispatcherServlet to the servlet container through org.springframework.web.context.support.AnnotationConfigWebApplicationContext. It only needs the root context to successfully register it to the ServletContext. Modify our SpringWebInitializer by overriding the onStartup() method where ServletContext is available for programmatical servlet registration:
@Configuration 
public class SpringWebinitializer implements 
      WebApplicationInitializer { 
     
     @Override 
     public void onStartup(ServletContext container) 
throws ServletException { 
       addDispatcherContext(container); 
     } 
    
     private void addDispatcherContext(ServletContext container) { 
       // Create the dispatcher servlet's ROOT context 
       AnnotationConfigWebApplicationContext dispatcherContext = 
            new AnnotationConfigWebApplicationContext(); 
       dispatcherContext.register(SpringDispatcherConfig.class); 
    
       // Declare  <servlet> and <servlet-mapping> for the 
       // DispatcherServlet 
       ServletRegistration.Dynamic dispatcher = 
            container.addServlet("ch02-servlet", 
               new DispatcherServlet(dispatcherContext)); 
       dispatcher.addMapping("*.html"); 
       dispatcher.setLoadOnStartup(1); 
     } 
} 
  1. Save all the files. Then build and deploy the project and verify if errors are encountered. If none, add the following view resolvers to our root context SpringDispatcherConfig to manage our views later:
@Bean 
    public InternalResourceViewResolver jspViewResolver() { 
        InternalResourceViewResolver viewResolverA = 
new InternalResourceViewResolver(); 
        viewResolverA.setPrefix("/WEB-INF/jsp/"); 
        viewResolverA.setSuffix(".jsp"); 
        viewResolverA.setOrder(1); 
        return viewResolverA; 
    } 
    
   @Bean 
   public ResourceBundleViewResolver bundleViewResolver(){ 
      ResourceBundleViewResolver viewResolverB = 
new ResourceBundleViewResolver(); 
      viewResolverB.setBasename("config.views"); 
      viewResolverB.setOrder(0); 
      return viewResolverB; 
} 
  1. Create the same views.properties, JSP pages, and just as we did in the previous recipe:
  2. To close this JavaConfig project setup, apply class-level annotations @EnableWebMvc and @ComponentScan to our SpringDispatcherConfig root context definition class to finally recognize all the classes and interfaces in each package as a valid Spring @Component ready for auto-wiring:
@EnableWebMvc 
@ComponentScan(basePackages="org.packt.starter.ioc") 
@Configuration 
public class SpringDispatcherConfig extends 
   WebMvcConfigurerAdapter{ 
    
   @Bean 
    public InternalResourceViewResolver jspViewResolver() { 
        InternalResourceViewResolver viewResolverA = 
new InternalResourceViewResolver(); 
        viewResolverA.setPrefix("/WEB-INF/jsp/"); 
        viewResolverA.setSuffix(".jsp"); 
        viewResolverA.setOrder(1); 
        return viewResolverA; 
    } 
    
   @Bean 
   public ResourceBundleViewResolver bundleViewResolver(){ 
      ResourceBundleViewResolver viewResolverB = 
new ResourceBundleViewResolver(); 
      viewResolverB.setBasename("config.views"); 
      viewResolverB.setOrder(0); 
      return viewResolverB; 
   } 
} 
  1. Save all files. Then build and deploy the project.