Spring MVC 3.0 Framework


Published on

Spring MVC 3.0 Framework

1. Introduce Spring MVC Module
2. Learn about Spring MVC Components (Dispatcher, Handler mapping, Controller, View Resolver, View)

1. What Is Spring?
2. Why use Spring?
3. By the way, just what is MVC?
4. MVC Architecture
5. Spring MVC Architecture
7. Spring MVC Components
8. DispatcherServlet
9. DispatcherServlet Architecture.........

Published in: Technology, Education

Spring MVC 3.0 Framework

  1. 1. copyright © I-AdminSpring Framework 3.0 MVCPrepared By:Ravi Kant SoniSr. Software Engineer | ADS-Bangaloresession - 1
  2. 2. copyright © I-AdminObjectives Introduce Spring MVC Module Learn about Spring MVC Components(Dispatcher, Handler mapping, Controller,View Resolver, View)
  3. 3. copyright © I-AdminWhat Is Spring?
  4. 4. copyright © I-AdminWhat is Spring? Spring is light weight, open source framework beginsdeveloped by Spring Source Company Spring 1.0 was released in 2004 Spring is available on www.springframework.org It provides support for JPA, Hibernate, Web services,Schedulers, Ajax, Struts, JSF and many otherframeworks
  5. 5. copyright © I-AdminWhy use Spring? Spring was created to reduce the complexityobserved in Java enterprise application development Spring is organised into many modules that offervarious types of assistance to the developer in a hostof application areas• Spring enables developers to develop enterprise-class applications using POJOs• Testing an application written with Spring is simplebecause environment-dependent code is moved intothis framework
  6. 6. copyright © I-AdminBy the way, just what is MVC? Model-View-Controller or MVC is an architecturalpattern used in development of applications.– Model encapsulates the raw data– Controller responds to events, and instruct the model and view toperform actions based on events– View render information supplied by model in a form suitable foruser interaction.
  7. 7. copyright © I-AdminMVC Architecture
  8. 8. copyright © I-AdminSpring MVC Architecture Based on existing Servlet/JSP technology
  9. 9. copyright © I-AdminSpring MVC Components DispatcherServlet responsible for intercepting therequest and dispatching for specific urls. Controller responsible for processing user requestsand building appropriate model and passes it to theview for rendering View interface represents presentation logic and isresponsible for rendering content ModelAndView class objects encapsulates view andmodel linking Model encapsulates the application data, will consistof POJO
  10. 10. copyright © I-AdminDispatcherServlet Used to handle all incoming requests and route themthrough Spring Also responsible to initialize the frameworkscomponents which are used to process the requestat various stages Uses customizable logic to determine whichcontrollers should handle which requests Forwards all responses to through view handlers todetermine the correct views to route responses to DispatcherServlet Architecture uses the FrontController Design Pattern
  11. 11. copyright © I-AdminDispatcherServlet Architecture
  12. 12. copyright © I-AdminDispatcherServlet Mapping Request that are to be handled by DispatcherServletare to be mapped using a URL mapping in web.xmlconfig file. Each DispatcherServlet has its ownWebApplicationContext
  13. 13. copyright © I-AdminDispatcherServlet in web.xml Dispatcher Servlet named "spring" that will intercept all urls to this webapplication <servlet-mapping> tag indicates what URLs will be handled by the whichDispatcherServlet<servlet><servlet-name>spring</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>spring</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
  14. 14. copyright © I-AdminSpring Configuration By default Spring looks for a servletname -servlet.xml file in /WEB-INF For the previous example we would need to create a file in /WEB-INF namedspring-servlet.xml<beans xmlns …….><mvc:annotation-driven/><context:component-scan base-package="com.iadmin.spring" /><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/WEB-INF/jsp/" /><property name="suffix" value=".jsp" /></bean></beans>
  15. 15. copyright © I-AdminSpring Configuration cont.. <mvc:annotation-driven /> tells Spring to supportannotations like @Controller, @RequestMappingand others that simplify the writing and configurationof controllers InternalResourceViewResolver resolve the viewnames, looks for JSPs that match a given viewname in the director /WEB-INF/jsp <context:component-scan...> tell Spring where toautomatically detect controllers
  16. 16. copyright © I-AdminThe ‘C’ in MVC DispatcherServlet delegates the request to thecontrollers to execute the functionality specific to it The @Controller annotation indicates that aparticular class serves the role of a controller Controller interpret user input and transform thisinput into specific model which will be represented tothe user by the view
  17. 17. copyright © I-AdminThe ‘M’ in MVC Model is generally defined as a MAP that cancontain objects that are to be displayed in view ModelAndView object encapsulates the relationsbetween view and model and is returned bycorresponding Controller methods ModelAndView class use ModelMap that is customMAP implementation where values are added in key-value fashion
  18. 18. copyright © I-AdminThe ‘V’ in MVC View Page can be explicitly returned as part ofModelAndView object by the controller In case of mapping logical name of view can beresolved to particular view page in caseModelAndView doesn’t contain the view reference The view name can be independent of viewtechnology (without using .jsp in controller) andresolved to specific technology by usingViewResolver and render by View
  19. 19. copyright © I-AdminDefining a Controller @Controller annotation defines the class as aSpring MVC controller @RequestMapping annotation is used to map aURL to either an entire class or a particular handlermethod DispatcherServlet delegates the request to thecontrollers to execute the functionality specific to it
  20. 20. copyright © I-AdminDefining a Controller cont…@Controllerpublicclass HelloController{@RequestMapping(value = "/hello", method = RequestMethod.GET)public String printHello(ModelMap model) {model.addAttribute("message", "Hello Spring MVC !");return "hello";}} The value attribute indicates the URL to which the handlermethod is mapped The method attribute defines the service method to handleHTTP GET request
  21. 21. copyright © I-AdminCreating JSP Views Spring MVC supports many types of views for different presentationtechnologies. These include - JSPs, HTML, PDF, Excel worksheets, XML,Velocity templates, XSLT, JSON, Atom and RSS feeds, JasperReports etc /WEB-INF/hello/hello.jsp:<html><head><title>Hello Spring MVC</title></head><body><h2>${message}</h2></body></html> Here, ${message} is the attribute which we have setup inside the Controller
  22. 22. copyright © I-AdminSpring vs. Struts Struts Framework– Based on MVC architecture– View – JSPs, ActionForms– Controller – ActionServlet, Actions Spring Framework– DispatcherServlet - Spring’s Front Controller implementation– View- JSP’s– ViewResolver - Maps logical View names to actual Viewimplementations– HandlerMapping-Strategy interface used by DispatcherServlet formapping incoming requests to individual Controllers
  23. 23. copyright © I-AdminDEMO
  24. 24. copyright © I-AdminQuestionsThank Youravikant.soni@i-admin.com