The Spring MVC configuration

The Spring MVC framework can be configured with XML files or Java configuration classes. We will configure our application using Spring MVC configuration classes, the first being the WebAppConfig class:

package com.gieman.tttracker.web;

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.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@EnableWebMvc
@Configuration
@ComponentScan("com.gieman.tttracker.web")
public class WebAppConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new UserInSessionInterceptor())
                .addPathPatterns(new String[]{
                    "/**"
                }).excludePathPatterns("/security/**");
    }
}

The WebAppConfig class extends WebMvcConfigurerAdapter, which is a convenient base class that provides empty implementations for each of the WebMvcConfigurer interface methods. We override the addInterceptors method to register our UserInSessionInterceptor and define the handler mappings that will be used to apply the interceptor. The path pattern /** will intercept all the mappings from which we exclude the /security/** mappings. The security mappings should not include a user session check because the user has not yet been authenticated and will not be in session.

The @ComponentScan("com.gieman.tttracker.web") annotation will trigger a scan for @Controller annotated classes in the com.gieman.tttracker.web package. Our handler classes will then be identified and loaded by Spring. The @EnableWebMvc annotation identifies this class as a Spring web MVC configuration class. This annnotation results in Spring loading the required WebMvcConfigurationSupport configuration properties. The remaining @Configuration annotation identifies the class as a candidate for component scanning during Spring application startup. The WebAppConfig class is then automatically loaded for use in the Spring MVC container.

The WebAppConfig class configures the MVC environment; it is the WebApp class that configures the servlet container:

package com.gieman.tttracker.web;

import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class WebApp extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    @Override 
    protected String[] getServletMappings() {
        return new String[]{
            "/ttt/*"
        };
    }
    
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class<?>[0];
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class<?>[]{WebAppConfig.class};
    }
}

The AbstractAnnotationConfigDispatcherServletInitializer class was introduced in Spring 3.2 as a base class for WebApplicationInitializer implementations. These implementations register a DispatcherServlet configured with annotated classes as defined in the WebAppConfig class (note how this class is returned in the getServletConfigClasses method).

The final configuration item of interest is the getServletMappings method, which maps incoming requests to the WebAppConfig set of handlers that are discovered via the @ComponentScan annotation. Every URL in our application that starts with /ttt/ will be directed to an appropriate request handler for processing. Some example URLs submitted from an Ext JS 4 client could include the following:

  • /ttt/company/findAll.json will map to the CompanyHandler.findAll method
  • /ttt/project/find.json?idProject=5 will map to the ProjectHandler.find method

Note that the /ttt/ prefix in the URL defines the entry point to our Spring MVC components. URLs that do not start with /ttt/ will not be handled by the Spring MVC container.

We will now implement one more handler to introduce data binding in Spring controllers.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.51.157