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.
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.
- 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
EmployeeControllerwhich have three different methods for all operation.
- 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.
- HandlerExceptionResolver – Spring MVC provides
HandlerExceptionResolverinterface that we can implement to create a global exception handler.
- Controller Based – We can define exception handler methods in our controller classes.
- Global Exception Handler – Spring provides
@ControllerAdviceannotation 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
- Spring MVC framework designed on JSP-Model 2 Architecture.
DispatcherServletis used as a front controller in Spring MVC and the front controller is used to map multiple actions to a single controller.
- The front controller
- 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 5 MVC Hello World using XML configuration
- Spring depends on attribute example
- Spring MVC CRUD Example using JdbcTemplate + MySQL
- How to inject array in Spring
- Getting Started with Spring Security