Create Spring MVC Maven Project in Eclipse

In this post I will talk about the general steps for creating a Spring MVC maven project in Eclipse. For this you need Eclipse for Java EE Developers with Maven Integration for Eclipse and Spring IDE plugins.

And of course a Tomcat or any other Java EE server to run the project on.

spring mvc maven project

Spring MVC Maven Project using Eclipse

In short, creating a Spring MVC maven project breaks up to:

  1. Creating a maven project with the webapp archetype.
  2. Adding the spring-webmvc, jstl and taglibs standard dependencies.
  3. Mapping the Dispatcher servlet to an url pattern.
  4. Configuring the beans (controller’s package, view resolver) (Spring configuration file).
  5. Writing the controller and the jsps.

1. Go to File > New > Other…

Click Maven Project then Next.

spring mvc maven project

2. Accept the default settings or change the location if you need to. Click Next.

spring mvc maven eclipse workspace

3. Change the Catalog to Internal. Write webapp inside Filter: field and select the option as in the screenshot. Click Next.

spring mvc maven archetype

4. Write the artifact id. Click Finish.
Artifact id means the name of your application.

spring mvc maven artifact id

5. After the project was created you might have an error. Let’s fix it.

spring mvc maven project eclipse

This error is caused by not having jsp-api.jar and servlet-api.jar libraries on the build path. This can be solved by either adding them in Maven’s pom.xml (with scope: provided) or simply add a targeted runtime to the project.

spring mvc maven targeted runtime

You open the above dialog by right clicking on the project > Properties
By adding a targeted runtime you basically add to build path some jars (libraries) that are needed but they are provided by the server under which the application will reside. provided maven scope does exactly just that. It adds the jars to the build path but doesn’t package them in your war because the server already has them.

6. Go inside web.xml. We will change the specification from 2.3 (very old) to 2.5 (still old, but way better).

spring mvc maven web.xml

Change your web.xml to look like this. The important/changed parts are highlighted.

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

</web-app>

7. Go inside pom.xml. We will add the barely minimum required dependencies for any Spring MVC maven project such as spring-webmvc and jstl.

Make your pom look like this:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.superglobals</groupId>
  <artifactId>SpringMVC-Example</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringMVC-Example Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
	    <groupId>org.springframework</groupId>
	    <artifactId>spring-webmvc</artifactId>
	    <version>4.2.6.RELEASE</version>
	</dependency>
	<dependency>
	    <groupId>javax.servlet</groupId>
	    <artifactId>jstl</artifactId>
	    <version>1.2</version>
	</dependency>
	<dependency>
	  <groupId>taglibs</groupId>
	  <artifactId>standard</artifactId>
	  <version>1.1.2</version>
	</dependency>
  </dependencies>
  <build>
    <finalName>SpringMVC-Example</finalName>
  </build>
</project>

8. Go back inside web.xml to write the servlet. This is standard Java EE configuration.
We are telling that we have a servlet called MyFirstSpringMVCServlet. It’s class is org.springframework.web.servlet.DispatcherServlet.
This class was made by the guys who developed spring and is included in one of the jars listed under Maven Depedencies.

Next we add a url mapping for this server. I added “/” because I want this servlet to be mapped to the root of my application. This means even the first request should go through it.

<web-app xmlns="http://java.sun.com/xml/ns/javaee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">

    <servlet>
        <servlet-name>MyFirstSpringMVCServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>MyFirstSpringMVCServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

9. Run the app on server. You will get the following error:

org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/MyFirstSpringMVCServlet-servlet.xml]

By default Spring is looking for a configuration file in /WEB-INF/ with the following name pattern: -servlet.xml.
Since our servlet is called MyFirstSpringMVCServlet, Spring is looking for a MyFirstSpringMVCServlet-servlet.xml file which we don’t have. Yet.

9. Create a new Spring Configuration xml inside the WEB-INF folder.
Right click on WEB-INF > New > Other…”
spring mvc configuration file

10. In the next screen select the following namespaces. We need them because some tags in our configuration are defined in them. Click Finish.

spring mvc maven configuration tags

11. Inside MyFirstSpringMVCServlet-servlet.xml write the following:

  • On line 9 we tell Spring that beans are annotation driven. This means we don’t need to define them here. Just annotate them accordingly in the Java classes.
  • On line 10 we tell Spring to search inside net.superglobals package (not yet created) for classes with annotation such as @Controller.
  • Lines 13 – 19 contain the configuration for the view resolver. In controller methods when we return a string such as “index” the resolver will search inside the prefix (aka jsps directory) for files named the same as that string and ending with .jsp extension.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
	
	<mvc:annotation-driven/>
	<context:component-scan base-package="net.superglobals"/>
	
	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix">
			<value>/WEB-INF/jsps/</value>
		</property>
		<property name="suffix">
			<value>.jsp</value>
		</property>
	</bean>
</beans>

10. Create a new folder inside src > main called java (src/main/java). In it create the net.superglobals package. Inside WEB-INF create a folder called jsps.

11. Inside the package create the following Java class:

  • @Controller has “/” as value because we want the method inside to run everytime the application is accessed.
  • Method name can be anything.
  • We add to the model an attribut (a string with the name message). We will retrieve this later in our jsp page.
  • We return “index” meaning that Spring will return the following page: WEB-INF/jsps/index.jsp
package net.superglobals;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller("/")
public class MyController {
	
	@RequestMapping(method = RequestMethod.GET)
	public String testMethod(Model model) {
		model.addAttribute("message", "Testing Spring MVC");
		return "index";
	}
}

12. Create index.jsp inside WEB-INF/jsps/ folder.

  • We use the jstl taglib to manipulate model attributes.
  • We retrieve the message model attribute that we set earlier in the controller.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
	${message}
</body>
</html>

Run the application on the server. If we go to

http://localhost:8080/SpringMVC-Example/

You should see the following message:

Testing Spring MVC

By now you should have a fully working Spring MVC maven project created with Eclipse :).

FacebookTwitterLinkedin