This guide walks through setting up a Spring MVC project using Maven within the Eclipse IDE.
1. Create a Maven Web Project
- In Eclipse, select File > New > Other > Maven > Maven Project.
- Keep the default settings and click Next.
- Choose the archetype
maven-archetype-webappand leave other options as default. - Fill in the project details (e.g., Group Id:
com.test, Artifact Id:HelloSpringMVC) and click Finish.
After creation, the project structure appears similar to this:
HelloSpringMVC/
├── src/
│ ├── main/
│ │ ├── java/
│ │ ├── resources/
│ │ └── webapp/
│ │ ├── WEB-INF/
│ │ │ └── web.xml
│ │ └── index.jsp
│ └── test/
│ └── java/
└── pom.xml
Note: index.jsp may show an error because the Servlet API is missing. The error is: The superclass "javax.servlet.http.HttpServlet" was not found on the Java build path. To resolve this, add the Servlet dependency to pom.xml or configure the Tomcat runtime library in the project's build path.
Option 1: Add Servlet dependency to pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
Option 2: Add Tomcat runtime library
- Right-click the project > Build Path > Configure Build Path.
- Go to the Libraries tab, click Add Library, choose Server Runtime, and select your Tomcat server.
After fixing the error, the project is ready for Spring MVC configuration.
2. Configure Spring MVC
2.1 Add Spring dependencies in pom.xml
Update the pom.xml file:
<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>com.test</groupId>
<artifactId>HelloSpringMVC</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>HelloSpringMVC Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>4.1.1.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
</dependencies>
<build>
<finalName>HelloSpringMVC</finalName>
</build>
</project>
Save the file; Maven will download the required JARs.
2.2 Configure web.xml
Edit src/main/webapp/WEB-INF/web.xml:
<web-app version="3.0" 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_3_0.xsd">
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
2.3 Create Spring configuration file
Create src/main/resources/springContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<!-- Enable component scanning for controllers -->
<context:component-scan base-package="com.test" />
<!-- Configure view resolver -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
- The
base-packageattribute tells Spring where to scan for annotated controllers. - The view resolver maps logical view names to physical JSP files. For example,
hellospringresolves to/WEB-INF/views/hellospring.jsp.
2.4 Create a Controller and View
Create the controller class src/main/java/com/test/HelloSpringController.java:
package com.test;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class HelloSpringController {
String message = "Welcome to Spring MVC!";
@RequestMapping("/hello")
public ModelAndView showMessage(
@RequestParam(value = "name", required = false, defaultValue = "Spring") String name) {
ModelAndView mv = new ModelAndView("hellospring");
mv.addObject("message", message);
mv.addObject("name", name);
return mv;
}
}
@Controllermarks the class as a Spring MVC controller.@RequestMappingmaps web requests to handler methods.@RequestParambinds request parameters to method arguments.ModelAndViewholds the view name and model data. The view namehellospringcombined with the prefix and suffix yields/WEB-INF/views/hellospring.jsp.
Update src/main/webapp/index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 4 MVC - HelloWorld Index Page</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h3>
<a href="hello?name=zhangsan">Click to go</a>
</h3>
</center>
</body>
</html>
Create the view page at src/main/webapp/WEB-INF/views/hellospring.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Spring 4 MVC - HelloWorld</title>
</head>
<body>
<center>
<h2>Hello World</h2>
<h2>${message} ${name}</h2>
</center>
</body>
</html>
The ${message} and ${name} are Expression Language (EL) expressions that display the model attributes added in the controller.
3. Deploy and Run on Tomcat
- Right-click the project > Run As > Run on Server. Choose your Tomcat server.
- Access the application at:
http://localhost:8080/HelloSpringMVC/orhttp://localhost:8080/HelloSpringMVC/index.jsp- Click the link to see the mapped view:
http://localhost:8080/HelloSpringMVC/hello?name=zhangsan
Important: If the JSP displays ${message} ${name} literally instead of the values, the JSTL library is missing. Add thece dependencies to pom.xml:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
After adding them, the EL expressions should work correctly.