Introduction

The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications.

The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between these elements.

MVC is a framework methodology that separates an application's code implementation into three components model, view, and controller. Each component contains different application logic.

Spring MVC is built on the Spring Inversion of control(IoC) framework. Spring decouples the MVC components and simplifies the whole MVC configuration.

Figure: Spring MVC Component interaction diagram

    • User requests are sent to Controller. Controller communicates with Service to perform business operations and will set return data or other data into Model object and forward control to View.

  • View gets data from Model object and displays at Page.

  • Model is a carrier of data between Controller and View.

  • Service communicates to DAO to make database operations. Service performs business operations and handles transactions.

  • DAOs are implemented in ORM or JDBC and communicate to database. DAO performs CRUD operations.

The DispatcherServlet

The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlet that handles all the HTTP requests and responses. The request processing workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram:

Following is the sequence of events corresponding to an incoming HTTP request to DispatcherServlet:

    • After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the appropriate Controller.

    • Controller is called with the help of URL mapped to GET or POST request. Controller calls object of Service class to perform required business operation. After finishing services call, Controller stores data into model and returns View name to DispatcherServlet.

    • The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request.

    • Once the view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser. View takes data from model to display at view page.

All the above mentioned components ie. HandlerMapping, Controller and ViewResolver are parts of WebApplicationContext which is an extension of the plain ApplicationContext with some extra features necessary for web applications.

Components

Defining Controller

Controllers are defined with help of @Controller annotation. The @Controller annotation indicates that a particular class serves the role of a controller. The @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.

@Controller

@RequestMapping(value = "/Welcome")

public class WelcomeCtl {

@RequestMapping(method = RequestMethod.GET)

public String display(Model model) {

System.out.println("I am in display");

model.addAttribute("message", "Welcome to Spring MVC!!");

return "Welcome";

}

..

}

The @Controller annotation defines the class as a Spring MVC controller. Here, annotation @RequestMapping before class maps \Welcome url to this controller.

Next annotation @RequestMapping(method = RequestMethod.GET) before login() method is used to declare the display() method as the controller's default service method that handles HTTP GET request.

Last @RequestMapping(method = RequestMethod.POST) before submit() method is used to declare the submit() method as the controller's service method that handles HTTP POST request.

The @Controller mechanism also allows you to create RESTful Web sites and applications by using @PathVariable annotation and other features.

Creating JSP Views

Spring MVC supports different types of views for different presentation technologies that includes JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON, Atom and RSS feeds, JasperReports etc. But most commonly we use JSP templates written with JSTL.

Here is a sample code that created JSP view for Welcome page. As per view resolver Welcome page will be located as /WEB-INF/pages/Welcome.jsp:

<html>

<head>

<title>Hello Spring MVC</title>

</head>

<body>

<h2>${message}</h2>

</body>

</html>

Here ${message} is the model attribute which we have added from controller using model.addAttribute() method. You may add more attributes in model to display at View.

Model

Model is a map object that contains data in form of key and value pair. Model carries data from Controller to View. View gets data from Model using keys and displays them using JSTL and other Spring tags. Here Model carries "message" from Controller to View. Model is passed as parameter to a Controller method.

Following are the two model maps provided by Spring:

    1. org.springframework.ui.Model

    2. org.springframework.web.servlet.ModelAndView

FAQ

Q: Can you override DispatcherServlet ?

A: Yes we can override it to implement Front Controller.

Q: What is the function of the Handler Mapping object?

A: It routes the mapping URL to the respective controller class and its method.

Q: What is the function of View Resolver?

A: It resolves the view path and renders the view.

Q: Which View resolver you have used in your application?

A: UrlBasedViewResolver

Q: What is the function of a Model?

A: It carries data from Controller to View and View to Controller. Model object is injected in a method of controller as a parameter.

Q: What is the naming convention of a configuration file?

A: servletname-servlet.xml

Q: How do you inject Model object into a method of controller?

A: Using the method argument we will pass model object to method of controller.

Q: If you create dispatcher-servlet.xml file in SpringMVC then do you create ApplicationContext.xml file ?

A: No ApplicationContext.xml is created for a non-web standalone application.