Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

Spring MVC using Java based configuration

  • Difficulty Level : Hard
  • Last Updated : 01 Jun, 2020

Prerequisites: MVC Design Pattern, Spring MVC with JSP View

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. The model object can be passed between view and 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 make applications.

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

The Spring MVC framework is comprised of the following components:

  • Model: A model can be an object or collection of objects which basically contains the data of the application.
  • View: A view is used for displaying the 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. @Controller annotation is used to mark that class as controller.
  • Front Controller: It remains responsible for managing the flow of the web application. DispatcherServelet acts as a front controller in spring MVC.

Requirements : 

  • Eclipse (EE version).
  • Tomcat Apache latest version.

Steps to set up the Spring MVC application in eclipse IDE:

  • Step 1: Go to File menu and click on New -> Maven Project.
  • Step 2: Then in the search bar, type maven. Click on Maven Project and click Next.
  • Step 3: The default settings should remain as it is. Click Next.
  • Step 4: Select maven-archetype-webapp for web application and click Next.
  • Step 5: Give some Group id as well as Artifact id.
  • Step 6: Right click on the project and go to Properties.
  • Step 7: Click on targeted runtimes. Select the installed apache tomcat in your system. Click on apply -> apply and close.
  • Step 8: The java files should be in src/main/java folder to build spring MVC project. Go to src folder in the project. Right click on main and click on New -> Folder.
  • Step 9: Write java as folder name.
  • Step 10: Create a java class inside com.geesforgeeks.springmvc under src/main/java folder. The directory structure should look as follows.

    Steps to implement a Spring web MVC application using java based configuration:

    • Step 1: Initially, we need to create a POM.XML file. This file contains the maven dependencies of the spring framework to use them in the project.


        <name>SpringMVCjava Maven Webapp</name>

    • Step 2: The project contains a web.xml file which receives all the requests from the client. We will use in place of web.xml. getServletMappings() function receive all the requests corresponding to the ‘/ ’ URL mapping. The function getServletConfigClasses() configures the dispatcher servlet and transfers the handler to dispatcher servlet java file MVCconfig.class. file is used in place of dispatcher servlet for java based configuration. This class should extend AbstractAnnotationConfigDispatcherServletInitializer class to serve the purpose of web.xml file in java based configuration. This file is defined as:


      package com.geeksforgeeks.web;
      import org.springframework.web

      public class WebInitializer
          extends AbstractAnnotationConfigDispatcherServletInitializer {
          protected Class<?>[] getRootConfigClasses()
              // TODO Auto-generated method stub
              return null;
          protected Class<?>[] getServletConfigClasses()
              // TODO Auto-generated method stub
              return new Class[] { MVCconfig.class };
          protected String[] getServletMappings()
              // TODO Auto-generated method stub
              return new String[] { "/" };

    • Step 3: Now, we need to create a file. This file is used in place of dispatcher servlet file. Annotating a class with the @Configuration indicates that the class can be used by the Spring IoC container as a source of bean definitions. To enable autodetection of the annotated controllers, it is required to add component scanning to the configuration. It also gives the path of base package (i.e com.geeksforgeeks.web) wherein controller files need to be searched. This class should extend WebMvcConfigurerAdapter class to serve the purpose of dispatcher servlet.


      package com.geeksforgeeks.web;

      import org.springframework.context
      import org.springframework.context
      import org.springframework.web.servlet

      @ComponentScan({ "com.geeksforgeeks.web" })

      public class MVCconfig
      extends WebMvcConfigurerAdapter {

    • Step 4: Now, we need to define a controller. Controllers interpret user input and transform it into a model that is represented to the user by the view. The @RequestMapping annotation is used to map the URLs such as “/greet” onto an entire class or a particular handler method. We create an object of ModelAndView class. setViewName() indicates the view to be called and addObject() indicates the dynamic content to be added to that object.


      package com.geeksforgeeks.web;
      import org.springframework
      import org.springframework.web
      import org.springframework.web
      public class GreetController {
          public ModelAndView showview()
              ModelAndView mv = new ModelAndView();
                           "GeeksForGeeks Welcomes "
                               + "you to Spring!");
              return mv;

    • Step 5: Now, we need to create an index page for the application. It is the page which is displayed when the given URL is hit. So, we create the index.jsp file as follows:


      <h2>Hello World!</h2>
          <form action="greet">
              <input type="submit" value="Press to greet">

    • Step 6: When the press to greet button from the above defined index.jsp is pressed, Result.jsp page opens up. So, we need to define the Result.jsp file:


      <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
          pageEncoding="ISO-8859-1" isELIgnored="false"%>
      <!DOCTYPE html>
              <meta charset="ISO-8859-1">
              <title>Insert title here</title>


    • When index.jsp is opened:

    • When the button is pressed:

    My Personal Notes arrow_drop_up
Recommended Articles
Page :