Skip to content
Related Articles

Related Articles

Spring MVC Application Without web.xml File

View Discussion
Improve Article
Save Article
  • 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>
    <url>http://maven.apache.org</url>
    <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: 

  1. Create a dispatcher servlet object
  2. 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

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




// Java Program to Illustrate
// CalculatorApplicationInitializer Class
  
package com.geeksforgeeks.calculator.config;
  
// Importing required classes
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;
  
// Class
public class CalculatorApplicationInitializer
    implements WebApplicationInitializer {
  
    public void onStartup(ServletContext servletContext)
        throws ServletException
    {
  
        XmlWebApplicationContext webApplicationContext
            = new XmlWebApplicationContext();
        webApplicationContext.setConfigLocation(
            "classpath:application-config.xml");
  
        // Creating a dispatcher servlet object
        DispatcherServlet dispatcherServlet
            = new DispatcherServlet(webApplicationContext);
  
        // Registering Dispatcher Servlet with Servlet
        // Context
        ServletRegistration
            .Dynamic myCustomDispatcherServlet
            = servletContext.addServlet(
                "myDispatcherServlet", dispatcherServlet);
  
        // Setting load on startup
        myCustomDispatcherServlet.setLoadOnStartup(1);
  
        // Adding mapping url
        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




// Java Program to Illustrate GfgController Class
  
package com.geeksforgeeks.calculator.controllers;
  
// Importing required classes
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
  
// Class
@Controller
public class GfgController {
  
    @RequestMapping("/welcome")
    @ResponseBody
  
    // Method
    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"?>
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    
  <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.

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!