Spring MVC Application Without web.xml File
Last Updated :
20 Apr, 2022
Spring MVC framework enables separation of modules namely Model, View, and Controller, and seamlessly handles the application integration. This enables the developer to create complex applications also using plain java classes. Here we will be creating and running Your First Spring MVC Application, whenever we are Configuring Dispatcher Servlet we are configuring inside a file named “web.xml” file. But we don’t want this file. We want a Spring MVC application with java based configuration, and how to do it? So, here we are going to create and run a Spring MVC Application Without a web.xml File.
Step 1: Set up the project.
Note: We are going to use Spring Tool Suite 4 IDE for this project. Please refer to this article to install STS on your local machine How to Download and Install Spring Tool Suite (Spring Tools 4 for Eclipse) IDE?
Go to your STS IDE then create a new maven project, File > New > Maven Project, and choose the following archetype as shown in the below image as follows:
Step 2: Let’s Delete the web.xml file
Now, let’s delete the web.xml file and run your spring MVC application. We can see we are encountering the below problems as shown in the below image. So we have to write another configuration file instead of this XML file so that we can safely delete this file.
Step 3: Adding Some Maven Dependencies
Add the following maven dependencies and plugin to your pom.xml file.
<!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<!-- plugin -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
</plugins>
</build>
Below is the complete code for the pom.xml file after adding these dependencies.
File: pom.xml
XML
< modelVersion >4.0.0</ modelVersion >
< groupId >com.geeksforgeeks</ groupId >
< artifactId >spring-calculator</ artifactId >
< packaging >war</ packaging >
< version >0.0.1-SNAPSHOT</ version >
< name >spring-calculator 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 >5.3.18</ version >
</ dependency >
< dependency >
< groupId >javax.servlet</ groupId >
< artifactId >javax.servlet-api</ artifactId >
< version >4.0.1</ version >
< scope >provided</ scope >
</ dependency >
</ dependencies >
< build >
< finalName >spring-calculator</ finalName >
< plugins >
< plugin >
< groupId >org.apache.maven.plugins</ groupId >
< artifactId >maven-war-plugin</ artifactId >
< version >2.6</ version >
< configuration >
< failOnMissingWebXml >false</ failOnMissingWebXml >
</ configuration >
</ plugin >
</ plugins >
</ build >
</ project >
|
Step 3: Project Development
Before moving into the coding part let’s have a look at the file structure in the below image.
3.1: So at first create an src/main/java folder and inside this folder create a class named CalculatorApplicationInitializer and put it inside the com.geeksforgeeks.calculator.config package and implement the WebApplicationInitializer interface. Refer to the below image.
3.2: Now in this class, we have to perform the following 2 major operations as listed below as follows:
- Create a dispatcher servlet object
- Register Dispatcher Servlet with Servlet Context
And we can do it by writing these lines of code
A. Create a dispatcher servlet object:
XmlWebApplicationContext webApplicationContext = new XmlWebApplicationContext();
// Create a dispatcher servlet object
DispatcherServlet dispatcherServlet = new DispatcherServlet(webApplicationContext);
B. Register Dispatcher Servlet with Servlet Context:
ServletRegistration.Dynamic myCustomDispatcherServlet = servletContext.addServlet("myDispatcherServlet",
dispatcherServlet);
Go to the src/main/resources and create an XML file. Name the file as application-config and paste the below code inside this file.
File: application-config.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
</ beans >
|
And below is the complete code for the CalculatorApplicationInitializer.java file. This is the replacement of our web.xml file. Comments are added inside the code to understand the code in more detail.
File: CalculatorApplicationInitializer.java
Java
package com.geeksforgeeks.calculator.config;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class CalculatorApplicationInitializer
implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext)
throws ServletException
{
XmlWebApplicationContext webApplicationContext
= new XmlWebApplicationContext();
webApplicationContext.setConfigLocation(
"classpath:application-config.xml" );
DispatcherServlet dispatcherServlet
= new DispatcherServlet(webApplicationContext);
ServletRegistration
.Dynamic myCustomDispatcherServlet
= servletContext.addServlet(
"myDispatcherServlet" , dispatcherServlet);
myCustomDispatcherServlet.setLoadOnStartup( 1 );
myCustomDispatcherServlet.addMapping( "/gfg.com/*" );
}
}
|
Step 4: Create Controller and Test The Application
Go to the src/main/java folder and inside this folder create a class named GfgController and put it inside the com.geeksforgeeks.calculator.controllers package. Below is the code for the GfgController.java file.
Code: GfgController.java file
Java
package com.geeksforgeeks.calculator.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class GfgController {
@RequestMapping ( "/welcome" )
@ResponseBody
public String helloGfg()
{
return "Welcome to GeeksforGeeks!" ;
}
}
|
Before running the application add the below lines to the application-config.xml file.
<context:component-scan base-package="com.geeksforgeeks.calculator.controllers"></context:component-scan>
File: Updated application-config.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< context:component-scan base-package = "com.geeksforgeeks.calculator.controllers" ></ context:component-scan >
</ beans >
|
Step 5: Run The Application
Now run your spring MVC application and hit the following URL
http://localhost:8080/spring-calculator/gfg.com/welcome
and we can see the output as shown in the below image.

Similar Reads
Spring MVC - JSTL forEach Tag with Example
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
6 min read
Spring MVC - Iterating List on JSP using JSTL
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
6 min read
Two-Way Data Binding in Spring MVC with Example
Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
7 min read
Spring MVC - Basic Example using JSTL
JSP Standard Tag Library (JSTL) is a set of tags that can be used for implementing some common operations such as looping, conditional formatting, and others. JSTL aims to provide an easy way to maintain SP pages The use of tags defined in JSTL has Simplified the task of the designers to create Web
8 min read
Spring MVC @ModelAttribute Annotation with Example
In Spring MVC, the @ModelAttribute annotation binds a method parameter or method return value to a named model attribute and then exposes it to a web view. It refers to the property of the Model object. For example, if we have a form with a form backing object that is called "Student" then we can ha
8 min read
Data Transfer Object (DTO) in Spring MVC with Example
In Spring Framework, Data Transfer Object (DTO) is an object that carries data between processes. When you're working with a remote interface, each call is expensive. As a result, you need to reduce the number of calls. The solution is to create a Data Transfer Object that can hold all the data for
8 min read
Spring MVC - Capture and Display the Data from Registration Form
This article is the continuation of this article Spring MVC - Create Registration Form using Form Tag Library, where we have successfully created a registration form using the Form Tag Library. Here in this article, we are going to explain how can we capture the data that are entered by the user and
3 min read
Spring MVC - Create Registration Form using Form Tag Library
Spring Framework provides springâs form tag library for JSP views in Springâs Web MVC framework. In Spring Framework, we use Java Server Pages(JSP) as a view component to interact with the user. From version 2.0, Spring Framework provides a comprehensive set of data binding-aware tags. These tags ar
7 min read
Data Binding in Spring MVC with Example
Data Binding, as the name itself, is a self-explanatory word. In data binding what we have to do is we have to capture or store the data so that we can bind that data with another resource (for example displaying the data in the frontend part) as per our needs or we can also read the data from a var
8 min read
Spring MVC - Pagination with Example
We will be explaining how we can implement pagination in Spring MVC Application. This is required when we need to show a lot of data on pages. Suppose in an e-commerce site we have a lot of products but we can't show all of those on a single page, so we will show only 20 products on each page. This
3 min read