Spring Mvc
Spring Mvc
Separates the domain (Model), the user interface (View), and the actions
based on user input (Controller) into three separate categories:
3
Spring MVC
Spring Framework
Spring 2.5
uses annotations
Earlier versions on introduced a
like @Controller,
Spring (Before 2.5) simplified,
@RequestMapping,
relied more on xml annotation-based
and so on, to mark
configurations model for
a class as a
developing
Controller or to map
Spring MVC
a request to its
applications
handler respectively
informally referred
to as “Spring
@MVC”
4
@Controller
• @Controller
• Indicates that a particular class serves the role
of a controller.
• Eliminates the need to extend any Controller
Request and
base class Response are
automatically
filled in by Spring
MVC
5
@Controller
• Controllers interpret user input and convert it into a model
object that can be used by view.
• @Controller is a stereotype annotation and hence,
dispatcher servlet can automatically scan for these classes.
• To enable auto-detection use the following in dispatcher-
servlet.xml file.
<context:component-scan base-package=“com.xyz.controllers.*"/>
5
@RequestMapping
• @RequestMapping
• Is used to map URLs like '/userlogin.htm’ to an
entire class or a particular handler method.
• Typically, class-level annotation maps a specific
request pattern
6
@RequestMapping
– Handler methods annotated with @RequestMapping have
very flexible method signatures.
– The methods can accept the following types of parameters
• HttpServletRequest
• HttpServletResponse
• ModelAttribute
• ModelMap
• BindingResult
• Errors
– The following return types are supported
• ModelAndView
• ModelMap
• ModelAttribute
• String
5
Components
DispatcherServlet Acts as a Front Controller
Controller Class This class will return the Model And View
Note: *JSP is not part of Spring but it is commonly used to create a user interface in Spring
applications
7
Spring MVC Flow
Spring MVC
DispatcherServlet (1 of 3)
• DispatcherServlet:
• Is the core of Spring MVC
• Acts as a Front Controller
– Coordinates all request handling activities
– Dispatches to registered handlers for processing a
web request.
10
Spring MVC
DispatcherServlet (2 of 3)
• Spring MVC’s front controller
(DispatcherServlet) coordinates the entire
request lifecycle:
• Configured in web.xml:
11
Spring MVC
DispatcherServlet (3 of 3)
13
Spring MVC
Application Flow (2 of 4)
1. DispatcherServlet receives
request and coordinates
business functionality.
14
Spring MVC
Application Flow (3 of 4)
2. DispatcherServlet looks for
the xml configuration file and
calls appropriate controller
15
Spring MVC
Application Flow (4 of 4)
16
Spring MVC
ModelAndView
• ModelAndView contains the model (some data) and either a logical view
name, or an implementation of the View interface.
17
Spring MVC
View Resolvers
• Logical view names become view objects.
• InternalResourceViewResolver resolves a
logical view name…
18
View
• Once the ViewResolver resolves the logical
‘view name’ with the corresponding physical
‘view implementation file’, the target-view is
returned to DispatcherServlet that displays the
view to the end-user (web client).