Spring MVC Framework

Several MVC Web frameworks such as Struts, Wickets, and Tapestry can be used for creating Web applications.

The Spring framework also has its own MVC implementation, the Spring MVC Web framework.

This framework has the following features that make it better than the other MVC frameworks

  • Pluggable view technology
  • Injection of services into controllers
  • Integration support

The Spring Web MVC framework

Is a highly robust, flexible, and well-designed framework that is used for rapidly developing Web applications using the MVC design pattern.

Takes advantage of the AOP and DI features of the Spring framework to enable you to create loosely coupled Web applications.

Is built around a front controller servlet called dispatcher servlet.

The Spring MVC framework makes use of the following components while processing a user request

  • Handler mappings
  • Controllers
  • View resolvers
  • View

Handler mappings :Enables the dispatcher servlet to forward the incoming requests to the appropriate controllers.

Controllers :Provide the application logic to process the incoming user request and generate the appropriate response.

View resolvers :Are used for resolving the logical view names returned by the controller to the actual views.

View :Is used to display the desired response to the user on the browser screen.

AdvantagesOf Spring MVC

  • Ease of testing
  • Reusable application code
  • Simple and powerful tag library
  • Supports multiple view technologies and Web frameworks
  • Light-weight development environment

Life Cycle of a Web Request

Spring MVC

Steps To Handle web request in Spring-enabled MVC Web application

  • Create and configure a dispatcher servlet
  • Create the controller class that performs the business logic for the requested page.
  • Configure the controller within the dispatcher servlet's context configuration file.
  • Declare a view resolver to tie the controller with the view.
  • Create a view to render the requested page to the user.

Dispatcher Servlet

When a user requests for a particular page, the request is received by a front controller at the server.

The front controller is a servlet called dispatcher servlet and is represented by the org.springframework.web.servlet.DispatcherServlet class.

The Dispatcher Servlet class is integrated with the Spring framework's ApplicationContext interface.

This integration allows you to use major features, such as DI and AOP,of the Spring framework.

The dispatcher servlet intercepts all user requests before passing them to a controller class.

For a dispatcher servlet to intercept all user requests, declare and configure it in the web.xml configuration file with the help of the following elements:

  • <servlet>
  • <servlet-mapping>
Example
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>