160 likes | 466 Views
Spring 3.0 MVC - Introduction. By, Srinivas Reddy.S. www.JAVA9S.com. Advantages of Spring 3.0 MVC. Supports RESTful URLs. Annotation based configuration. Supports to plug with other MVC frameworks like Struts etc.
E N D
Spring 3.0 MVC - Introduction By, SrinivasReddy.S www.JAVA9S.com
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 Controller Request 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 DispactherServlet (Front controller) Controller (Responsible to handle request) 3 Request 1 4 Model (POJO) 5 View (JSP, XML, Velocity) www.JAVA9S.com
Spring 3.0 MVC Request Flow Request Response DispatcherServlet HandlerChain Interceptor - Pre Process Capture the Request Locale Interceptor - Pre Process Prepare the View If request is multipart- File upload data is exposed Controller Interceptor - Post Process HandlerMapping (Map of URL and controllers) Interceptor - Post Process View Resolver
Spring 3 MVC Framework-Initialization DispatcherServlet -Initialization MultipartResolver LocaleResolver ThemeResolver HandlerMappings HandlerAdapters HandlerExceptionResolvers RequestToViewNameTranslator ViewResolvers
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" /> </beans> www.JAVA9S.com