Sie sind auf Seite 1von 13

Spring 3.

0 MVC Introduction

www.JAVA9S.co

By, Srinivas

Supports RESTful URLs. Annotation based configuration. Supports to plug with other MVC frameworks like Struts etc. Flexible in supporting different view types like www.JAVA9S.co JSP, velocity, XML, PDF

Advantages of Spring 3.0 MVC

MVC An overview
Model
Request

Controller

Response
www.JAVA9S.co

View

Front Controller

Front Controller

www.JAVA9S.co

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

Spring 3 MVC- Basic Architecture HandlerMapping


2
(Map of URL and controllers)

Dispacth Request erServlet


1 (Front controller)

Controller
(Responsible to handle request)

4 5 View (JSP, XML, Velocity) Model (POJO)

www.JAVA9S.co

Request

Spring 3.0 MVC Request FlowResponse


DispatcherServlet
Interceptor Interceptor Pre Process Pre Process Controller Intercepto r Post Interceptor Process Post Process

multipart- File upload data is exposed HandlerMapping


(Map of URL and controllers)

Capture the Request Locale If request is

Handle rChain

Prepar e the View View Resolve r

Spring 3 MVC Framework-Initialization


MultipartResolver

Dispatche rServlet Initializatio n

LocaleResolver ThemeResolver HandlerMappings HandlerAdapters


HandlerExceptionResolver s
RequestToViewNameTranslator

ViewResolvers

Interface

Default bean purpose name org.springframework. handlerMappingMaps the Request web.servlet.HandlerMa to pping Handlers(Controller s) org.springframework. none Plugs the other web.servlet.HandlerAd frameworks apter handlers org.springframework. viewResolver Maps the view web.servlet.ViewResol names to view ver instances org.springframework. handlerExceptioMapping of the web.servlet.HandlerExce nResolver exceptions to ptionResolver handlers and views

Important Intefaces

Step 1:

Spring 3.0 MVC Configuration

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

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.co m

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" /> </beans>

www.JAVA9S.co

Thank you visit www.java9s.com

Das könnte Ihnen auch gefallen