Spring MVC Framework

In this article, we will discuss Spring MVC Framework in detail. Later, we will extend this concept to implement few examples

 

Advantages of Spring MVC:

  • Helps in building flexible & loosely-coupled web application
  • With M-Model, V-View & C-Controller – there is a clear segregation of code for different logics
    1. Model – normally represented as POJO & encapsulates the application data
    2. View – renders the response to the front-end user for the request handled
    3. Controller – takes care of request processing on behalf of end user and constructs appropriate model to reply back to the calling user (requested user)
  • This framework designed around central servlet called DispatcherServlet for handling HTTP request/responses
  • Also, this framework can be integrated with other popular frameworks such as Hibernate, Struts2, JSF, etc
  • This framework supports different view technologies with few simple configurations in the spring configuration XML
  • Since Spring 2.5, we can configure and use this framework to build highly flexible MVC application using annotations (The default handler is based on the @Controller and @RequestMapping annotations)
  • Since Spring 3.0, with few more annotation spring supports REST web services
  • For more details, refer spring docs

 

Features of Spring MVC:

  • Clear segregation of code for different logics like for example presentation logic, business logic & navigation logic
  • Since Spring 3.0 building REST web services made easy with few annotations
  • No need to extend any specific interfaces/classes for handler or controller, just few annotation on top of the user-defined classes like @Controller, @RequestMapping, @RequestMethod, etc
  • Models are transferred in the form of name-value map with any view technology
  • For more details, refer spring docs

 

Dispatcher Servlet:

Spring MVC Framework designed around central servlet called DispatcherServlet which dispatches the HTTP requests to appropriate controller with the help handlers. Spring’s DispatcherServlet completely integrated with IOC container, this helps in leveraging the full use of spring features.

For more details refer, spring docs

SpringMVC

Source: Spring official site

Request Flow execution steps:

  • Client sends HTTP requests
  • These requests are intercepted by “DispatcherServlet”, which tries to figure out appropriate  handler mapping
  • DispatcherServlet consults Handler Mapping and invokes the Controller associated with the request
  • Now, the controller processes the requests invoking appropriate service method and returns the response/result (i.e.; ModelAndView instance which contains model data and view name) back to the DispatcherServlet
  • Then DispatcherServlet  sends the view name from ModelAndView instance to view resolver object
  • Time to show output to the user: view name along with model data will render the response/result back to the user

Note:DispatcherServlet” also called as Front Controller (Front-Controller design pattern)

Sample web.xml in web application

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>SpringMVC</display-name>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>*.htm</url-pattern>
	</servlet-mapping>

</web-app>

web.xml

  • DispatcherServlet is the entry point to the HTTP requests
  • This is a normal servlet class which extends HTTPServlet base class
  • In the above configuration, servlet name is spring-mvc
  • On loading, dispatcher servlet looks for the file name [servlet-name]–servlet.xml under WEB-INF folder of the web application. This is default look up, we can override with user-defined servlet file name
  • In our example, dispatcher servlet
    1. looks for spring-mvc-servlet.xml file under WEB-INF directory
    2. intercepts any HTTP requests with url pattern ending *.htm

 

Conclusion: We have got a good introduction about Spring MVC framework, its advantages and features it supports in the later versions. We also deep-dived to understand the execution flow for Spring MVC framework designed around the central servlet called “DispatcherServlet”

With this, we can now move on to implement few basic examples to get good hands-on experience in spring MVC framework

 

Read Also:

 

Happy Coding !!
Happy Learning !!

Spring MVC - Creating Maven-based web application in Eclipse IDE