Spring MVC Framework Overview - Spring MVC Framework

What is Spring - MVC Framework Overview?

MVC stands for Model-View-Controller. Spring MVC framework provides this architecture and components. Using this MVC framework, loosely coupled web applications are developed with ease. MVC pattern is designed such a way that it separates the input logic, business logic and UI logic.

  • The Model binds the application data which consists POJO
  • The View is responsible in delivering the model data by generating HTML output. This output will be interpreted by Client’s browser.
  • The Controller takes the user requests and creates a model and sends it t the view for delivering it to User.

The DispatcherServlet

All HTTP requests and responses are handled by DispatcherServlet which is designed by Spring MVC framework. Following diagram illustrates the request processing workflow of DispatcherServlet.

Spring  MVC Framework Overview

Below are the events in sequence which corresponds to an HTTP request to DispatcherServlet.

  • Upon receiving HTTP request, HandlerMapping is consulted by DispatcherServlet to call appropriate Controller.
  • Requests are taken by Controller and it calls the service methods depending on GET or POST methods. The service method sets the model data implementing business logic and returns the view name of DispatcherServlet.
  • The View of the request is picked up by ViewResolver which helps the DispatcherServlet.
  • The DispatcherServlet send the model data to the view once it’s finalized, which is delivered to the browser.
  • HandlerMapping, Controller and ViewResolver components mentioned above are the parts of WebApplicationContext. It is an extension to the plain ApplicationContext which has extra features.

Required Configuration

Requests are mapped using a URL mapping in the web.xml file which DispatcherServlet wants to handle. Below example shows the declaration and mapping for HelloWeb DispatcherServlet example:

WebContent/WEB-INF directory of the web application will have web.xml file. Framework will load the application context from file which has a name structure [servlet-name]-servlet.xml which is under WebContent/WEB-INF directory upon initialization of HelloWeb DispatcherServlet. In the example, file name will be HelloWeb-servlet.xml.

DispatcherServlet handles the URLs which are mentioned in <servlet-mapping> tag. In this example, all the HTTP requests which are ending with .jsp are handled by HelloWeb DispatcherServlet.

We can even customize the default file name and location which are [servlet-name]-servlet.xml and WebContent/WEB-INF respectively by adding servlet listener i.e., ContextLoaderListener in web.xml file as below-

Keeping the points together, let us see the required configuration for our example HelloWeb-servlet.xml file that is placed in web application’s WebContent/WEB-INF directory:

Below are the considerable points about HelloWeb-servlet.xml file -

  • With the help of [servlet-name]-servlet.xml file we create the beans, by overriding the bean definitions of any with same name in global scope.
  • Spring MVC annotation scanning capability is activated by <context:component-scan…> tag which allows us to make use of @Controller and @RequestMapping etc annotations.
  • In the above example, a logical view name hello is assigned to a view implementation which is located at /WEB-INF/jsp/hello.jsp. To resolve the view names, InternalResourceViewResolver will define rules.
  • In coming section, we will show you how to create actual components in MVC framework i.e., Model, View and Controller.

Defining a Controller

DispatcherServlet assigns the incoming HTTP request to the controllers. The annotation @Controller specifies a particular class which fulfill the role of a controller. To map a URL to an entire class or a particular handler method, the annotation @RequestMapping is used.

The annotation @Controller specifies the class as a Spring MVC Controller. Next, the @RequestMapping mentions the all handling methods on this controller which are relative to /hello path. We declare printHello() method as default service method to handle next annotation @RequestMapping(method=RequestMethod.GET) HTTP GET request. We have the feasibility to define other method for handling any POST request to the same URL.

We can rewrite the above controller to add additional attributes in @RequestMapping as below-

From the above, the URL to which handler method is mapped is indicated by value attribute. The method attribute on the other hand specifies the method, which handle the HTTP GET request. Few important points regarding the controller defined in above example-

  • Service method will contain required business logic. Using this service method, we can even call other methods if required.
  • Depending on the logic we define, a model is created within this method. Getter and setter are created to the attributes which are accessed by the view for final output. Example above creates a model with “message” as attribute.
  • The name of the view is returned from the service method as a String and the above example returns us a “hello” view name.

Creating JSP Views

There are many types of views for presentation technologies by Spring MVC which are HTML, Excel worksheets, PDF, JSPs, XSLT, JSON, Velocity templates, Atom and RSS feeds etc. Most commonly used are JSP which are written with JSTL. Let us create a hello view in /WEB-INF/hello/hello.jsp:

In Controller, we have setup ${message} as the attribute. We can define many attributes inside our view to display.

All rights reserved © 2020 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Spring MVC Framework Topics