Spring MVC using Java Based Configuration
Spring MVC framework enables the separation of modules, namely Model, View, and Controller, and seamlessly handles application integration. This enables the developer to create complex applications using plain Java classes. The model object can be passed between the view and the controller using maps.
In this article, we will see how to set up a Spring MVC application in the Eclipse IDE and understand how to develop applications.
Spring MVC Framework Components
- Model: A model can be an object or a collection of objects that contains the application's data.
- View: A view is used for displaying information to the user in a specific format. Spring supports various technologies like FreeMarker, Velocity, and Thymeleaf.
- Controller: It contains the logical part of the application. The @Controller annotation is used to mark a class as a controller.
- Front Controller: It is responsible for managing the flow of the web application. DispatcherServlet acts as a front controller in Spring MVC.
Requirements
- Eclipse (EE version).
- Tomcat Apache latest version.
Prerequisites
Steps to Set Up the Spring MVC Application in Eclipse IDE
Step 1: Create a Maven Project
Go to File menu > Click on New > Select Maven Project.

Step 2: Search for Maven Project
In the search bar, type maven, select Maven Project, and click Next.

Step 3: Keep Default Settings
Ensure the default settings remain unchanged and click Next.

Step 4: Select Maven Archetype
Select maven-archetype-webapp for web applications and click Next.

Step 5: Configure Group ID and Artifact ID
Provide a Group ID and Artifact ID.

Step 6: Configure Tomcat Runtime
Right-click on the project > Properties

Click on Targeted Runtimes > Select the installed Apache Tomcat > Click Apply and Close.

Step 7: Create Java Folder
Ensure Java files are in src/main/java to build a Spring MVC project.
- Go to the src folder in the project.
- Right-click on main and select New Folder.

Name the folder as java.

Step 8: Create Java Class
Create a Java class named "AddController" inside com.geeksforgeeks.springmvc under src/main/java.

Steps to Implement a Spring MVC Application Using Java-Based Configuration
Step 1: Create pom.xml
This file contains the Maven dependencies for the Spring framework.
<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.geeksforgeeks</groupId>
<artifactId>SpringMVCjava</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>SpringMVCjava Maven Webapp</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.12.RELEASE</version>
</dependency>
</dependencies>
<build>
<finalName>SpringMVCjava</finalName>
</build>
</project>
Step 2: Create WebInitializer.java
This file replaces web.xml. The getServletMappings() function receives requests corresponding to the '/' URL mapping. getServletConfigClasses() configures the dispatcher servlet and transfers the handler to MVCconfig.class.
package com.geeksforgeeks.web;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { MVCconfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}
Step 3: Create MVCconfig.java
This file replaces the dispatcher servlet. The @ComponentScan annotation enables component scanning.
package com.geeksforgeeks.web;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
@ComponentScan({ "com.geeksforgeeks.web" })
public class MVCconfig extends WebMvcConfigurerAdapter {}
Step 4: Create GreetController.java
This controller handles the /greet request.
package com.geeksforgeeks.web;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class GreetController {
@RequestMapping("/greet")
public ModelAndView showView() {
ModelAndView mv = new ModelAndView();
mv.setViewName("result.jsp");
mv.addObject("result", "GeeksForGeeks Welcomes you to Spring!");
return mv;
}
}
Step 5: Create index.jsp
This is the landing page of the application.
<html>
<body>
<h2>Hello World!</h2>
<form action="greet">
<input type="submit" value="Press to greet">
</form>
</body>
</html>
Step 6: Create result.jsp
This page is displayed when the button in index.jsp is pressed.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>${result}</h1>
</body>
</html>
Output:
- When index.jsp is opened:
- When the button is pressed, result.jsp displays the greeting message.