0% found this document useful (0 votes)
3 views

Spring Mvc

The document outlines the Model View Controller (MVC) design pattern, which separates application logic into three components: Model, View, and Controller. It details the Spring MVC framework, including the use of annotations like @Controller and @RequestMapping for handling requests and mapping URLs. The document also describes the role of DispatcherServlet as the front controller that coordinates request handling and the application flow within a Spring MVC application.

Uploaded by

Bhanu Gannamani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Spring Mvc

The document outlines the Model View Controller (MVC) design pattern, which separates application logic into three components: Model, View, and Controller. It details the Spring MVC framework, including the use of annotations like @Controller and @RequestMapping for handling requests and mapping URLs. The document also describes the role of DispatcherServlet as the front controller that coordinates request handling and the application flow within a Spring MVC application.

Uploaded by

Bhanu Gannamani
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Spring MVC

Model View Controller Design Pattern


Model View Controller (MVC): A fundamental design pattern for separating
user interface logic from business logic

Separates the domain (Model), the user interface (View), and the actions
based on user input (Controller) into three separate categories:

Model Handles the behavior and data of the application

View Handles the display of information

Interprets the user inputs, informing the model and/or


Controller the view to change as appropriate

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.*"/>

• The component-scan tag also allows us to exclude /


include packages during auto-detection

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

Execute this method


to process requests
for /userlogin.htm.

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

The class of the object that will be used to


CommandClass represent the user data. This is also called Form
Backing Bean

Customizable view resolution class Application


ViewResolver Context Configuration file

JSP* Acts as 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:

• Is mapped using a URL Pattern. In this


case, .htm

11
Spring MVC
DispatcherServlet (3 of 3)

• Spring MVC’s front controller (DispatcherServlet)


coordinates the entire request lifecycle:
• All requests matching .htm pattern automatically
will be mapped to DispatcherServlet

• DispatcherServlet loads Spring application context


from an XML file* that usually contains <bean>
definitions for the Spring MVC components

• Note: *default is <servlet-name>-servlet.xml


12
Spring MVC
Application Flow (1 of 4)

The application flow is:

1. DispatcherServlet receives request and


coordinates business functionality
2. Looks for xml configuration file and calls
appropriate controller
3. Controller maps the request to handler, handler
processes request/returns instance of
ModelAndView to DispatcherServlet

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

• Use the following in the .xml configuration file to


send the DispatcherServlet looking for Controller(s).

15
Spring MVC
Application Flow (4 of 4)

3. Controller maps the request to appropriate handler


which in turn processes the request and returns
instance of ModelAndView to DispatcherServlet.

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).

You might also like