Monday, February 10, 2014

Spring MVC 4 - Java Config

Building a Maven pom.xml to work with Spring 4, Spring Security, Hibernate 4, and Thymeleaf has been time consuming and full of errors.  After a lot of time trying to retrofit Spring Security and Thymeleaf into my existing project, I decided to start with a simple project to test these technologies.  A link to the code on GitHub is provided at the bottom of this post.

1. Create a new Spring project ("Spring MVC Project") using STS in Eclipse.

2. Update the pom.xml to match the pom on the page of this blog.

3. Verify that the vanilla application runs on Tomcat in Eclipse.  I am using an external install of Tomcat 7.0.50 as shown below.

      If it runs successfully, you should see the page:

4. If successful, remove the Spring XML configuration files:

5. The next step is to create the two configuration files (, under the config directory below.  The other files and directories are not relevant at this stage.

6. The files should look like the following:
package dtr.oas.config;

import org.springframework.core.annotation.Order;

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

 protected Class< ? >[] getRootConfigClasses() {  
  return new Class[] { }; 

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

 protected String[] getServletMappings() {  
  return new String[] { "/" };  


package dtr.oas.config;

import org.springframework.context.annotation.Bean;
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.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;

public class WebAppConfig extends WebMvcConfigurerAdapter {

 public UrlBasedViewResolver setupViewResolver() {
  UrlBasedViewResolver resolver = new UrlBasedViewResolver();
  return resolver;

7. Run the application on Tomcat again and you should see the success page again.

Code at GitHub:


Toàn Lư Cự said...

May I ask if there is any changes when deploy to Geronimo? As I can't run it as I deleted all the xml configuration files and added the 2 files Initializer and WebAppConfig.
The url when deployed is http://localhost/tlc

Dave R. said...

When you export the project from Eclipse, you should export it as a war file. This will result in the file spring-ex01.war being created.

This file can be deployed to any Servlet 3.0 compliant container as is. You can try deploying this file to a stand-alone Tomcat server for verification. I checked this with Tomcat 7.0.50, but it should work the same with the current release of Tomcat (7.0.59).

If the deployment works with Tomcat, then you will need to determine if your version of Geronimo is Servlet 3.0 compliant, and if there are any Geronimo configuration settings that you need to change to activate Servlet 3.0.

Lastly, take a look at one of my prior comments on this topic:

Post a Comment