- Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.3:: Worker - Spring MVC without Web.xml usage 1
- Spring4Netty without Web.XML configuration on Java 8 (JBoss WildFly and Tomcat 8) - part.4:: Base - Spring MVC without Web.xml usage 2
It shows how to modify WebConfig.class (from github miko-s4netty) to link project static resources as Images, AngularJS App, CSS, JavaScript libraries or etc.
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:
Let's give *.jsp pages access to the static resources and consider following project structure (Img.1.)
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 WebMvcConfigurer. WebMvcConfigurer is normally imported by adding @EnableWebMvc annotation to @Configuration application class. The result it then delegated to WebMvcConfigurationSupport which provides Java-based config possibility.
The final result of WebConfig class according to the front-end project structure (Img.1.) can looks like following example.
@Configuration
@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 |
1. WebConfig extends WebMvcConfiguredAdapter which give us possibility to directly override necessary methods from WebMvcConfigurer. WebMvcConfigurer 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:
Post a Comment