Spring MVC Application Without web.xml File
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.
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.
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.
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
Before running the application add the below lines to the application-config.xml file.
File: Updated application-config.xml
Step 5: Run The Application
Now run your spring MVC application and hit the following URL
and we can see the output as shown in the below image.