Spring MVC

A Spring MVC is a Java framework that is used to build web applications. It follows the Model-View-Controller design pattern. It implements all the basic features of a core spring framework like Inversion of Control, Dependency Injection.

A Spring MVC provides an elegant solution to use MVC in the spring framework with the help of DispatcherServlet. Here, DispatcherServlet is a class that receives the incoming request and maps it to the right resource such as controllers, models, and views.

Spring Web Model-View-Controller

  • Model — A model contains the data of the application. Data can be a single object or a collection of objects.
  • Controller — A controller contains the business logic of an application. Here, the @Controller annotation is used to mark the class as the controller.
  • View — A view represents the provided information in a particular format. Generally, JSP+JSTL is used to create a view page. Although spring also supports other view technologies such as Apache Velocity, Thymeleaf, and FreeMarker.
  • Front Controller — In Spring Web MVC, the DispatcherServlet class works as the front controller. It is responsible to manage the flow of the Spring MVC application.

Understanding the flow of Spring Web MVC

  • As displayed in the figure, all the incoming request is intercepted by the DispatcherServlet that works as the front controller.
  • The DispatcherServlet gets an entry of handler mapping from the XML file and forwards the request to the controller.
  • The controller returns an object of ModelAndView.
  • The DispatcherServlet checks the entry of view resolver in the XML file and invokes the specified view component.

Model

We have a simple model class with a single variable and its getter-setter methods. It’s a simple POJO class.

Controller

The DispatcherServlet delegates the request to the controllers to execute the functionality specific to it. The @Controllerannotation 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.

The @Controller annotation defines the class as a Spring MVC controller. Here, the first usage of @RequestMapping indicates that all handling methods on this controller are relative to the /hello path. Next annotation@RequestMapping(method = RequestMethod.GET) is used to declare theprintHello() method as the controller’s default service method to handle HTTP GET request. You can define another method to handle any POST request at the same URL.

You can write the above controller in another form where you can add additional attributes in @RequestMapping as follows −

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 −

  • You will define the required business logic inside a service method. You can call another method inside this method as per requirement.
  • Based on the business logic defined, you will create a model within this method. You 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.

Creating JSP Views

Spring MVC supports many types of views for different presentation technologies. These include — 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.

Let us write a simple hello view in /WEB-INF/hello/hello.JSP −

Here ${message} is the attribute that we have set up inside the Controller. You can have multiple attributes to be displayed inside your view.

Features Of Spring MVC

  1. Clear separation of roles:- Each role like a controller, DispatcherServlet can be fulfilled by a specialized object.
  2. Adaptability, non-intrusiveness, and flexibility:- Define any controller method signature you need. (Such as @RequestParam)
  3. Customizable binding and validation:- Type mismatches as application-level validation errors that keep the offending value, localized date, and number binding, and so on instead of String-only form objects with manual parsing and conversion to business objects.