Spring 3.0 MVC - Introduction

Download Report

Transcript Spring 3.0 MVC - Introduction

Spring 3.0 MVC Introduction
www.JAVA9S.com
By,
Srinivas Reddy.S
Advantages of Spring 3.0 MVC
• Supports RESTful URLs.
• Annotation based configuration.
• Supports to plug with other MVC
frameworks like Struts etc.
• Flexible in supporting different view
types like JSP, velocity, XML, PDF
etc.,
www.JAVA9S.com
MVC – An overview
Model
Request
Controller
View
Response
www.JAVA9S.com
Front Controller
Front Controller
www.JAVA9S.com
Front Controller - Responsiblities
• Initialize the framework to cater to the
requests.
• Load the map of all the URLs and the
components responsible to handle the
request.
• Prepare the map for the views.
www.JAVA9S.com
Spring 3 MVC- Basic Architecture
HandlerMapping
(Map of URL and controllers)
2
Request
1
Dispacther
Servlet
Controller
3
(Responsible to
handle request)
(Front controller)
4
5
www.JAVA9S.com
View (JSP,
XML,
Velocity)
Model
(POJO)
Spring 3.0 MVC Request Flow
Request
Response
DispatcherServlet
Capture the Request
Locale
If request is
multipart- File upload
data is exposed
HandlerMapping
(Map of URL and controllers)
Interceptor Pre Process
Interceptor Pre Process
Handler Controller
Chain Interceptor -
Prepare
the View
Post Process
Interceptor Post Process
View
Resolver
Spring 3 MVC FrameworkInitialization
MultipartResolver
LocaleResolver
Dispatcher
Servlet
Initialization
ThemeResolver
HandlerMappings
HandlerAdapters
HandlerExceptionResolvers
RequestToViewNameTranslator
ViewResolvers
Important Intefaces
Interface
Default bean name
org.springframework.web.servlet handlerMapping
.HandlerMapping
org.springframework.web.servlet none
.HandlerAdapter
org.springframework.web.servlet viewResolver
purpose
Maps the Request to
Handlers(Controllers)
Plugs the other frameworks
handlers
.ViewResolver
Maps the view names to
view instances
org.springframework.web.servlet handlerExceptionRes
olver
.HandlerExceptionResolver
Mapping of the exceptions
to handlers and views
org.springframework.web.multip multipartResolver
art.MultipartResolver
Interface to handle the file
uploads
org.springframework.web.servlet localeResolver
.LocaleResolver
Helps to resolve the locale
from the request
org.springframework.web.servlet themeResolver
.ThemeResolver
Resolves a theme for a
Request.
Spring 3.0 MVC Configuration
• Step 1:
– Configure the web.xml with DispatcherServlet and
details of the application context file location.
<servlet>
<servlet-name>spring3</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring3</servlet-name>
<url-pattern>*.*</url-pattern>
</servlet-mapping>
www.JAVA9S.com
Spring 3.0 MVC Configuration
• Step 2:
– Configure the contextConfigLocation for the
application context to be loaded
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring3-service.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
www.JAVA9S.com
Spring 3.0 MVC Configuration
• Step 3: Configure the spring3-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.java9s.web" />
<bean class="org.springframework.web.servlet.mvc.annotation.
DefaultAnnotationHandlerMapping" />
<bean class="org.springframework.web.servlet.mvc.annotation.
AnnotationMethodHandlerAdapter" />
www.JAVA9S.com
</beans>
Thank you
visit
www.java9s.com