Friday, August 8, 2014

Spring 4 MVC without Web.XML configuration - static resources handling

This short post is the update to the previous ones: 
    Previously was only defined the way how to link symbolic links and components location  to scan which are normally defined by specific ServletContext *.xml file as in following example :
...
    
    
        
        
    
...
and we've transformed such *.xml file into the Java code in github project miko-s4netty:
@Configuration
@ComponentScan("com.miko.s4netty")
@EnableWebMvc
public class WebConfig {
    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}

Let's give *.jsp pages access to the static resources and consider following project structure (Img.1.)
Img.1. - part of project structure with static resources
From the Img.1. comes the necessity to link the two folders ("/app", "/lib") which will by available for the front-end development after the application deployment. In such case we need to extend the implementation of the WebConfig class and configure callback methods to correctly customise Java-based configuration of the project. 

1. WebConfig extends WebMvcConfiguredAdapter which give us possibility to directly override necessary methods from WebMvcConfigurerWebMvcConfigurer is normally imported by adding @EnableWebMvc annotation to @Configuration application class. The result it then delegated to WebMvcConfigurationSupport which provides Java-based config possibility. 
@Configuration
@ComponentScan("com.miko.s4netty")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
2. XML tag annotation for handling servlet requests <mvc:default-servlet-handler /> needs to be reflected in the WebConfig class by overriding  configureDefaultServletHandling method


...
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
   configurer.enable();
}
...
3. Also resources request handling needs to be reflected by overriding appropriate method in the class WebConfig. This is normally done by XML tags <mvc:resources/> 


...
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
   registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(31550522);
   registry.addResourceHandler("/lib/**").addResourceLocations("/lib/").setCachePeriod(31550522);
}
...
The last WebConfig class modification opens the possibility to continue building front-end application with appropriate resources. 
The final result of WebConfig class according to the front-end project structure (Img.1.) can looks like following example.
@Configuration
@ComponentScan("com.miko.s4netty")
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/app/**").addResourceLocations("/app/").setCachePeriod(31550522);
        registry.addResourceHandler("/lib/**").addResourceLocations("/lib/").setCachePeriod(31550522);
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public UrlBasedViewResolver setupViewResolver() {
        UrlBasedViewResolver resolver = new UrlBasedViewResolver();
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setViewClass(JstlView.class);
        return resolver;
    }
}
Enjoy building rich front-end application!

No comments: