Lesson 10 - Spring MVC
Lesson 10 - Spring MVC
Solution
To solve the above problems, Sun microsystem introduced two design patterns for developing
enterprise applications:
request 1
JSP
response
4
Enterprise
Browser 2 Server/Data
Source
3
JavaBean
Application Server
Model 1: Page Centric Model
WORKFLOW
request 1
Controller (Servlet)
response 2
5
Enterprise
browser 3 Model Server / Data
Java Bean Source
View (JSP)
4
Application Server
Model 2: MVC Model
WORKFLOW
1. Controller or Servlet receives request from browser and captures the input
2. Controller invokes the business method of the model or Java bean
3. Model connects with the database and gets business data
4. Model sends response to controller (Keeps the process data in heap memory request, session, and ServletContext)
5. Controller switches the control to appropriate view of the application
Model 2: MVC Model
LIMITATIONS
• Spring MVC is used to develop the web application that uses MVC design pattern
• Spring MVC is meant to make web application development faster, cost-effective, and flexible
Spring MVC Architecture
HandlerMapping
2. Request
3. Controller
5. Calls Business
1. Request Method Dispatcher
Dispatcher
Dispatcher 4. handlerRequest (req, res) Servlet Servlet
Service
Servlet Controller
11. ModelAndView 10. Response
16. Response
1. DispatcherServlet (org.springframework.web.servlet)
2. HandlerMapping (org.springframework.web.servlet)
3. Controller
4. ModeAndView (org.springframework.web.servlet)
5. ViewResolver
Components of Spring MVC
DispatcherServlet
1. It is given by org.springframework.web.DispatcherServlet
DispatcherServlet
2. It follows FrontController Design Pattern
3. Whatever URL comes from the Client, Servlet intercepts the Client Request before passing the
Request object to the Controller
HandlerMapping
4. In web configuration file, write <servlet-mapping> in such a way that Dispatcher Servlet is invoked
for ClientRequest
Controller
<servet>
<servlet-name> front-controller</servlet-name>
ModelAndView
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
</servlet>
ViewResolver
<servlet-mapping>
<servlet-name> front-controller </servlet-name>
<url-pattern>*.extensionname</url-pattern>
</servlet-mapping>
HandlerMapping • When a request is made to Spring’s dispatcher servlet, it hands over the request to
handler mapping.
Controller • Handler mapping inspects the request and identifies the appropriate handler execution
chain and delivers it to dispatcher servlet.
ModelAndView
ViewResolver
Components of Spring MVC
HandlerMapping: Example
Hander mapping provided by Spring’s MVC module can be implemented in many ways.
Lets take a example:
DispatcherServlet
BeanNameUrlHandlerMapping: It is the default handler mapping class, that maps the URL
HandlerMapping request to the names of the beans.
<bean…>
<bean class=“org.springframework.web.servlet.handler.
Controller BeanNameUrlHandlerMapping”/>
<bean name=“/welcome.htm” class=“WelcomeController”/>
ModelAndView
<bean name=“/streetName.htm” class=“StreetNameController”/>
<bean name=“/process*.htm” class=“ProcessController”/>
If URL pattern:
ViewResolver
• Controllers are components that are called by the Dispatcher Servlet for any kind of Business
DispatcherServlet
logic.
• All controllers implements Controller interface.
HandlerMapping
Types of Controllers:
Controller
1. AbstractController
2. MultiActionController
ModelAndView
3. AbstractwizardFormController
ViewResolver
There are components called ViewResolver. Their job is to provide mapping between the Logical
View Name and the actual Physical Location of the View Resource.
Components of Spring MVC
ModelAndView
ModelAndView
Example:
ViewResolver
ModelAndView mv=new ModelAndView(“successView”, “greetingMsg”, “greetingMessage);
Components of Spring MVC
ModelAndView
ModelAndView
ViewResolver
InternalResourceViewResolver
DispatcherServlet
It resolves the logical name of the view to an internal resource by prefixing the logical view name
with the resource path and suffixing it with the extension.
HandlerMapping
BeanNameViewResolver
Controller
It resolves the logical name of the view to the bean name, which will render the output to the user.
The bean should be defined in the Spring app context file.
ModelAndView
XMLFileViewResolver
ViewResolver
This view resolver is the same as BeanNameViewResolver. The only difference is that instead of
looking for the beans in Spring’s application context file, it looks for beans defined in a separate
XML file (/WEB-INF/views.xml by default).
Advanced Java
Topic 2—Spring MVC Program in Eclipse
Writing Spring MVC Program in Eclipse
Run the project We have already discussed the steps to create a web project in eclipse in the
previous lessons.
Writing Spring MVC Program in Eclipse
Create HelloWorldController.java (Controller)
Create dynamic web
project in eclipse Spring has Controllers. Here, AbstractController overrides the handleRequestInternal() method
Create web.xml ModelAndView(“HelloWorldPage”) identifies which view should return to the user. In this
(Deployment example, HelloWorldPage.jsp will be returned.
Descriptor)
model.addObject (“msg”, “Welcome”) adds a “welcome” string into a model named “msg.” You
Run the project can later use EL ${msg} to display the “hello world” string.
Writing Spring MVC Program in Eclipse
Create mvc-dispatcher-servlet.xml file. (Spring Configuration)
Create dynamic web
project in eclipse
Declare the Spring Controller and viewResolver (mvc-dispatcher-servlet.xml).
Create 'index.jsp'
(View)
<beans xmlns="http://www.springframework.org/schema/beans"
Create xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
HelloWorldController xsi:schemaLocation="http://www.springframework.org/schema/beans
.java (Controller) http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean name="/welcome.htm"
Create mvc-
class="HelloWorldController" />
dispatcher-
servlet.xml file. <bean id="viewResolver"
(Spring class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
Configuration) <property name="prefix">
<value>/WEB-INF/pages/</value>
Create web.xml </property>
(Deployment
<property name="suffix">
Descriptor)
<value>.jsp</value>
</property>
Run the project </bean>
</beans>
Writing Spring MVC Program in Eclipse
Create mvc-dispatcher-servlet.xml file. (Spring Configuration)
Create dynamic web
project in eclipse
Create web.xml
(Deployment
Descriptor)
Create
HelloWorldControll
er.java (Controller)
Create mvc-
dispatcher-
servlet.xml file.
(Spring
Configuration)
Create web.xml
(Deployment
Descriptor)
Spring MVC is used to develop the web application that uses MVC design
pattern. Spring MVC is used to make web application development faster, cost-
effective, and flexible