Spring Web MVC (Model View Controller) Introduction and Features


Spring Web MVC (Model View Controller) commonly known as Spring MVC is a module of Spring Framework. It helps to create the web-based application. Spring MVC developed on Servlet API and included from the beginning.

Spring MVC provides the support from view part to model part. On the top of IoC, we can execute the model classes and controller classes.

Spring Web MVC (Model View Controller) Introduction and Features

Features of Spring MVC

Form Backup Support– Form backup support help to hold the data into bean classes. It is applicable for view/presentation part. To achieve this we need to use Spring UI tags library.

Controller Support- Spring controller support divided into two parts.

  1. Multi-Form Controller- It helps to handle multiple form request from a single controller class. For example, if you have an employee registration, employee update, and employee logging form, all these forms can be handled by one single controller EmployeeController which have three different methods for all operation.
  2. Multi-Action Controller- If you have 3 different form like form1, form2, and form3, it can be also handled by one single controller. In this case, we need to store all forms of data in a session object and save it into the database by reading one by one from the session object.

Validation Support– Spring MVC provides support for form validation on the server. It can be achieved by @Validator annotation and BindingResult class through which we can get the errors raised by Validator implementation in the controller request handler method.

Interceptor Support- Spring interceptor support helps to intercept the user pre-processing and post-processing request. To do that Spring provides HandlerInterceptor interface in org.springframework.web.servlet package which has methods…

  • afterCompletion(...) – after the complete request has finished.
  • preHandle(...) – before the actual handler is executed.
  • postHandle(...) – after the handler is executed.

Exception Handling Support– Spring MVC exception can be handled by in three ways.

  1. HandlerExceptionResolver – Spring MVC provides HandlerExceptionResolver interface that we can implement to create a global exception handler.
  2. Controller Based – We can define exception handler methods in our controller classes.
  3. Global Exception Handler – Spring provides @ControllerAdvice annotation that we can use with any class to define our global exception handler.

i18n Support- Spring MVC also provides support for internationalization (i18n) or localization (L10n) better user interaction.

Key points to remember

  1. Spring MVC framework designed on JSP-Model 2 Architecture.
  2. DispatcherServlet is used as a front controller in Spring MVC and the front controller is used to map multiple actions to a single controller.
  3. The front controller DispatcherServlet belongs to org.springframework.web.servlet package.
  4. There is no recommended extension provided by Spring MVC, so you can use any extension like .htm, .ds, .wso, etc but most of the developer used .htm extension.

Project structure of Spring MVC application

For any Spring Web MVC application, all the resources must be arranged in a systematic manner like below…

Spring Web MVC (Model View Controller) Introduction and Features

References

  1. Validating Form Input
  2. Internationalization and localization
  3. Interface HandlerInterceptor

About the Author

Atul Rai
I like sharing my experiments and ideas with everyone by writing articles on the latest technological trends.