Monday, September 14, 2015

How to setup Spring MVC project with maven in Eclipse

There article shows how to create a spring MVC project with maven in Eclipse. The eclipse comes with an internal maven-archetype-webapp for webapps, but is somehow a little bit out of date. The project created by that needs some extra work to make it workable.

In this demo a maven managed Spring MVC helloworld  webapp is created in Eclipse. This demo doesn’t try to show how to use Spring MVC, but focus on how to set up the development environment in Eclipse.

0. What you need

  • JDK 1.7 +
  • Eclipse IDE for Jave EE (version 4.4 Luna)
  • Maven 3.2.1

1. Create a maven project in Eclipse

Create a new maven project.


Make sure choose the archetype maven-archetype-webapp


Then next until finish. You may notice that the new created project has errors. In Eclipse IDE, it has red crosses like below.


Don’t worry about that, we will deal with the errors step by step until it becomes a runnable one.

2. Configure the project

There are several things need to be changed for the eclipse created project.

2.1 Configure maven pom.xml

Since this is a maven managed project, the JDK version need to be set in pom.xml, not in Eclipse. The dependency Spring MVC is also added to pom.xml

<project xmlns="" 
<name>spring-mvc-helloworld Maven Webapp</name>

<!-- Use Spring MVC -->

<!-- Use Java 1.7 -->

After pom.xml saved and use Alt-F5 to update the maven project, Eclipse still complain about an error for the project like below.

It’s a error about “javax.servlet.http.HttpServlet” was not found on the Java Build Path.


Don’t worry, The class should be provided by the Servlet containers, such as Tomcat, we need to do more configuration for the running environment of the project. So let deal with it now.

2.2 Configure Dynamic Web Module for project

It’s a little bit tricky to change Dynamic Web Module version in Eclipse, first you need to unset the default old version 2.3 in project facets, then set it again with new version 3.0 or higher. Direct change the version number will cause an error in Eclipse like this.


So let do it in the correct way, unset the old one, then set it again with new version.

Right click on the project name – > choose properties. Uncheck the Dynamic Web Module 2.3, then hit OK to confirm.


Again, right click on the project name – > choose properties. Check the Dynamic Web Module 3.0, Also set specify a runtime server for the our helloworld webapp.


Now our helloworld Spring MVC web app don’t have any errors in Eclipse.


2.3 Add java directory

The wizard created project don’t have directory for java code, we need to create this directory by hand. Like other maven project,  /src/main/java is the folder need to be created to put the java code.

Now if you continue to proceed this tutorial, configure web.xml and dispatcher-servlet.xml, you will end up with a spring mvc project with configuration in XML file. If you prefer to have a spring mvc project with java-based configuration, you can skip the rest of this article and jump to another tuorial on Java-based Spring mvc configuration - without Spring security

2.4  basic web.xml configuration

Change the default /src/main/webapp/WEB-INF/web.xml to the basic configuration below .

<web-app xmlns="" 

<display-name>Demo for mvc hello world config</display-name>

<!-- specify location of spring config, don't use default --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:dispatcher-servlet.xml</param-value> </init-param> <load-on-startup>1</load-on-startup>


The version="3.0" means using servlet version 3.0. the Spring configuration file is name "dispatcher-servlet.xml" in classpath, usually means /src/main/resources/dispatcher-servlet.xml. Just remember don't use the default value for contextconfigLocation, which will be /src/main/webapp/WEB-INF by default, to avoid hitting rocks when doing integration test later.

2.5 basic spring configuration

Add a new file /src/main/resources/dispatcher-servlet.xml. In the same directory as web.xml, the basic spring configuration looks like below.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""

<context:component-scan base-package="com.shengwang.demo" />


<!-- ================================== -->
<!-- 1. mapping static resources -->
<!-- ================================== -->
<mvc:resources location="/static-resources/css/" mapping="/css/**" cache-period="3600"/>
<mvc:resources location="/static-resources/img/" mapping="/img/**" cache-period="3600"/>
<mvc:resources location="/static-resources/js/" mapping="/js/**" cache-period="3600"/>

<!-- ================================== -->
<!-- 2. view resolver for JSP -->
<!-- ================================== -->
<bean id="viewResolver"
<property name="prefix" value="/WEB-INF/"/>
<property name="suffix" value=".jsp"/>

Change the base package to your own. In this demo it’s com.shengwang.demo. The static resources directory can be any directories relative to /src/main/webapp

Now the project looks like this.


Since this article focuses on how to setup project and development environment for spring mvc with maven in Eclipse. The spring mvc coding part, such controllers and business logic are omitted. Now your maven managed Spring MVC project is setup in Eclipse, ready for some java code and run.

Happy coding!


Post a Comment

Powered by Blogger.

About The Author

My Photo

Has been a senior software developer, project manager for 10+ years. Dedicate himself to Alcatel-Lucent and China Telecom for delivering software solutions.


Unordered List