All IT Courses 50% Off
JAVA Tutorials

What is Spring MVC?

Spring MVC is a Java framework that is used to develop flexible and loosely coupled web applications. It follows the MVC (Model View Controller) pattern.

Related image

It follows all the basic features of core spring like Dependency Injection, Inversion Control, etc.

MVC can be used in the Spring framework using DispatcherServlet. DispatcherServlet is a class that maps incoming requests to the right resources.

  • Model: It encapsulates the data of the application, which can be a single object or a collection of objects.
  • View: It represents the information in a particular format. JSP + JSTL is used to create views.
  • Controller: It contains the business logic of the application.
  • Front Controller: DispatcherServlet is used as a front controller.

How Spring MVC works?

  1. The incoming request is first received by the DispatcherServlet.
  2. The DispatcherServlet then consults the HandlerMapping and forwards the request to the Controller.
  3. The Controller calls the appropriate service methods and returns an object of ModelAndView containing the model data and view name.
  4. To find the View, the DispatcherServlet sends the view name to the ViewResolver.
  5. The DispatcherServlet now passes the model object to the View.
  6. The View then returns the result to the user.

Advantages of Spring MVC:

  • Separate roles: It separates each role as a model object, controller, ViewResolver, DispatcherServlet, etc.
  • Light weight: Spring MVC uses light weight servlet container.
  • Rapid Development: It provides a fast and parallel development.
  • Reusable Business code: Existing codes can be reused in the application.
  • Flexible Mapping: It contains some specific annotations for flexible mapping.
  • Easy to test: Spring MVC contains Java Beans, which makes it easy to test the application.
  • Powerful Configuration: It gives configuration for the framework as well as application classes.

Steps to create a Spring MVC project:

  • Either load the jar files (Spring core jar file, Spring web jar file, JSP + JSTL jar file) or add maven dependencies.
  • Create a controller class.
  • The entry of the controller should be provided in the web.xml file.
  • In a separate XML file, define Bean.
  • Output the message in a JSP file.
  • Start the server and deploy the project.

Example of Spring MVC:

  1. HelloWorldController Class
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HelloWorldController extends AbstractController {
    private String message;
    protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("welcomePage","welcomeMessage", message);
    public void setMessage(String message) {
        this.message = message;

2. Entry of controller class in web.xml

All IT Courses 50% Off
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="" xmlns="" xmlns:web="" xsi:schemaLocation="" id="WebApp_ID" version="2.5">
        <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class>

3. Define bean in the xml file

<bean id="viewResolver"
    class=" org.springframework.web.servlet.view. InternalResourceViewResolver" >
        <property name="prefix">
        <property name="suffix">
    <bean name="/welcome.htm" class="com.vaannila.HelloWorldController" >
        <property name="message" value="Hello World!" />

4. Displaying message in JSP page i.e. index.jsp

<p>Welcome to Spring MVC Tutorial</p>  


What is Spring MVC?

The files that are required to execute the above example are:


Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Articles

Back to top button