Spring MVC helps in building flexible and loosely coupled web applications. the model-view-controller design pattern helps in separating the business logic, presentation logic, and navigation logic. models are responsible for encapsulating the application data. the views render a response to the user with the help of the model object. controllers are responsible for receiving the request from the user and calling the back-end services.
Introduction to Spring Web MVC framework
The Spring Web model-view-controller (MVC) framework is designed around a DispatcherServlat that dispatches requests to handlers, with configurable handler mappings, view resolution, locale, and theme resolution as well as support for uploading files. The default handler is based on the @Controller and @RequestMapping annotations, offering a wide range of flexible handling methods. The@Controller mechanism also allows us to create RESTful Web sites and applications, through the @PathVariable annotation and other features.
A controller is typically responsible for preparing a model Map with data and selecting a view name but it can also write directly to the response stream and complete the request.
The model (the M in MVC) is a Map interface, which allows for the complete abstraction of the view technology. We can integrate directly with template-based rendering technologies such as JSP, Velocity, or directly generate XML, JSON, and many other types of content. The model Map is simply transformed into an appropriate format.
How Spring Web MVC Really Works
The figure below shows the flow of requests in the spring MVC framework.
when a request is sent to the spring MVC framework the following sequence of events happens.
- the dispatcher servlet first receives the request.
- the dispatcher servlet consults the helping and invokes the controller associated with the request.
- the controller processes the request by calling the appropriate service methods and returns a modelAndViewobject to the dispatcher servlet. the modelAndViewobject object contains the model data and the view name.
- the dispatcher servlet sends the view name to a view resolver to find the actual view to invoke.
- now, the dispatcher servlet will pass the model object to view to render the result.
- the view, with the help of the model data, will render the result back to the user.
Defining a Controller
The DispatcherServlet delegates the request to the controllers to execute the functionality specific to it. The @Controller annotation indicates that a particular class serves the role of a controller. The @ReaquestMapping annotation is used to map a URL to either an entire class or a particular handler method.
The @Controller annotation defines the class as a Spring MVC controller. Here, the first usage of @ReaquestMapping indicates that all handling methods on this controller are relative to the /hello path. Next annotation @RequestMapping(method = RequestMethod.GET) is used to declare the print hello() method as the controller’s default service method to handle HTTP GET requests.
The value attribute indicates the URL to which the handler method is mapped and the method attribute defines the service method to handle HTTP GET requests. The following important points are to be noted about the controller defined above −
- We will define the required business logic inside a service method. We can call another method inside this method as per requirement.
- Based on the business logic defined, we will create a model within this method. We can use setter different model attributes and these attributes will be accessed by the view to present the final result. This example creates a model with its attribute “message”.
- A defined service method can return a String, which contains the name of the view to be used to render the model. This example returns “hello” as a logical view name.
Spring MVC Model Class
We have a simple model class with a single variable and its getter-setter methods. It’s a simple POJO class.
Spring MVC supports many types of views for different presentation technologies. These include -JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON, etc.
In this article, we have gone through the processing of a request in the Spring MVC framework in detail. That’s all for the Spring MVC, I have tried to keep it as simple as possible.