Search This Blog

Tuesday, May 6, 2014

Spring Web MVC configuration using annotation

After configured Spring Application Initializer , we need to write one more class that will include all the configuration related to Spring web MVC. Once Spring web container will invoked it will search for the annotated class which have configuration like 'view resolver' , 'resource handler ' etc. To get more details on this please refer following code snippet-


 @Configuration
@EnableWebMvc
@ComponentScan(basePackages = ApplicationConstants.CONFIG_LOCATION)
public class WebConfig extends WebMvcConfigurerAdapter{

/**
* Default constructor
*/
public WebConfig() {

}

/**
* The view resolver for the application
* @return InternalResourceViewResolver
*/
@Bean
public InternalResourceViewResolver getInternalResourceViewResolver() {
   InternalResourceViewResolver resolver = new InternalResourceViewResolver();
   resolver.setPrefix("/WEB-INF/pages/");
   resolver.setSuffix(".jsp");
   return resolver;
}




/* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#addResourceHandlers(org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry)
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
 registry.addResourceHandler("/resources/**").addResourceLocations("/WEB-INF/");
 
}

/* (non-Javadoc)
* @see org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter#configureDefaultServletHandling(org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer)
*/
@Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }



}

Kindly comment if anything , I have missed here. I will update the same accordingly.

No comments :

Post a Comment