Spring MVC


Published on

Spring MVC be Dror Bereznitsky from AlphaCSP

Published in: Business, Technology
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Spring MVC

  1. 2. Spring MVC Dror Bereznitsky Senior Consultant and Architect, AlphaCSP It’s Time
  2. 3. Agenda <ul><li>Introduction </li></ul><ul><li>Background </li></ul><ul><li>Features Review </li></ul><ul><ul><li>Configuration </li></ul></ul><ul><ul><li>View technology </li></ul></ul><ul><ul><li>Page flow </li></ul></ul><ul><ul><li>Table sorting </li></ul></ul><ul><ul><li>Search results pagination </li></ul></ul><ul><ul><li>Validation </li></ul></ul><ul><ul><li>AJAX </li></ul></ul><ul><ul><li>Error handling </li></ul></ul><ul><ul><li>I18n </li></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><li>Summary </li></ul>
  3. 4. Introduction <ul><li>Spring’s web framework </li></ul><ul><ul><li>Optional Spring framework component </li></ul></ul><ul><ul><li>Integrated with the Spring IoC container </li></ul></ul><ul><li>Web MVC framework </li></ul><ul><ul><li>Request-driven action framework </li></ul></ul><ul><ul><li>Designed around a central servlet </li></ul></ul><ul><ul><li>Easy for Struts users to adopt </li></ul></ul>
  4. 5. Introduction:: Key Features <ul><li>Simple model </li></ul><ul><ul><li>Easy to get going - fast learning curve </li></ul></ul><ul><li>Designed for extension </li></ul><ul><ul><li>Smart extension points put you in control </li></ul></ul><ul><li>Strong integration </li></ul><ul><ul><li>Integrates popular view technologies </li></ul></ul><ul><li>A part of the Spring framework </li></ul><ul><ul><li>All artifacts are testable and benefit from dependency injection </li></ul></ul>
  5. 6. Introduction:: More Key Features <ul><li>Strong REST foundations </li></ul><ul><li>Data Binding Framework </li></ul><ul><li>Validation Framework </li></ul><ul><li>Internationalization Support </li></ul><ul><li>Tag Library </li></ul>
  6. 7. Introduction:: Full Stack Framework? <ul><li>Spring MVC is not a full-stack web framework, but provide the foundations for such </li></ul><ul><li>Spring MVC is not opinionated </li></ul><ul><ul><li>You use the pieces you need </li></ul></ul><ul><ul><li>You adopt the pieces in piece-meal fashion </li></ul></ul>
  7. 8. Introduction:: Spring 2.5 <ul><li>Released November 2007 </li></ul><ul><li>Simplified, annotation based model for developing Spring MVC applications </li></ul><ul><li>Less XML than previous versions </li></ul><ul><li>Focuses on </li></ul><ul><ul><li>ease of use </li></ul></ul><ul><ul><li>smart defaults </li></ul></ul><ul><ul><li>simplified programming model </li></ul></ul>
  8. 9. Agenda <ul><li>Introduction </li></ul><ul><li>Background </li></ul><ul><li>Features Review </li></ul><ul><ul><li>Configuration </li></ul></ul><ul><ul><li>View technology </li></ul></ul><ul><ul><li>Page flow </li></ul></ul><ul><ul><li>Table sorting </li></ul></ul><ul><ul><li>Search results pagination </li></ul></ul><ul><ul><li>Validation </li></ul></ul><ul><ul><li>AJAX </li></ul></ul><ul><ul><li>Error handling </li></ul></ul><ul><ul><li>I18n </li></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><li>Summary </li></ul>
  9. 10. Background:: Dispatcher Servlet <ul><li>Dispatcher Servlet - front controller that coordinates the processing of all requests </li></ul><ul><ul><li>Dispatches requests to handlers </li></ul></ul><ul><ul><li>Issues appropriate responses to clients </li></ul></ul><ul><li>Analogous to a Struts Action Servlet </li></ul><ul><li>Define one per logical web application </li></ul>
  10. 11. Background:: Request Handlers <ul><li>Incoming requests are dispatched to handlers </li></ul><ul><ul><li>There are potentially many handlers per Dispatcher Servlet </li></ul></ul><ul><ul><li>Controllers are request handlers </li></ul></ul>
  11. 12. Background:: ModelAndView <ul><li>Controllers return a result object called a ModelAndView </li></ul><ul><ul><li>Selects the view to render the response </li></ul></ul><ul><ul><li>Contains the data needed for rendering </li></ul></ul><ul><li>Model = contract between the Controller and the View </li></ul><ul><li>The same Controller often returns different ModelAndView objects </li></ul><ul><ul><li>To render different types of responses </li></ul></ul>
  12. 13. Background:: Request Lifecycle Copyright 2006, www.springframework.org Handler
  13. 14. Features Review <ul><li>Introduction </li></ul><ul><li>Background </li></ul><ul><li>Features Review </li></ul><ul><ul><li>Configuration </li></ul></ul><ul><ul><li>View technology </li></ul></ul><ul><ul><li>Page flow </li></ul></ul><ul><ul><li>Table sorting </li></ul></ul><ul><ul><li>Search results pagination </li></ul></ul><ul><ul><li>Validation </li></ul></ul><ul><ul><li>AJAX </li></ul></ul><ul><ul><li>Error handling </li></ul></ul><ul><ul><li>I18n </li></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><li>Summary </li></ul>
  14. 15. Features:: Configuration <ul><li>Old school Spring beans XML configuration </li></ul><ul><li>Annotation based configuration for Controllers (v2.5) </li></ul><ul><ul><li>XML configuration is still required for more advanced features: interceptors, view resolver, etc. </li></ul></ul><ul><ul><li>Not mandatory, everything can still be done with XML </li></ul></ul>
  15. 16. Deploy a DispatcherServlet <ul><li>Minimal web deployment descriptor </li></ul>web.xml <servlet> <servlet-name> spring-mvc-demo </servlet-name> <servlet-class> org.springframework.web.servlet.DispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> /WEB-INF/spring-mvc-demo-servlet.xml </param-value> </init-param> </servlet> Dispatcher servlet configuration
  16. 17. Annotated Controllers <ul><li>Simple POJO – no need to extend any controller base class ! </li></ul>@Controller public class PhoneBookController { @RequestMapping ( value = &quot;/phoneBook&quot; , method = RequestMethod. GET ) protected ModelAndView setupForm() throws Exception { ModelAndView mv = new ModelAndView(); … return mv; } PhoneBookController.java
  17. 18. Dispatcher Servlet Configuration <ul><li>/WEB-INF/spring-mvc-demo.xml </li></ul><ul><ul><li>Setting up the dispatcher for annotation support </li></ul></ul><ul><ul><li>Actually done by default for DispatcherServlet </li></ul></ul><ul><ul><li>Auto detection for @Controller annotated beans </li></ul></ul><bean class= &quot;org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping&quot; /> <bean class= &quot;org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter&quot; /> <context:component-scan base-package= &quot;com.alphacsp.webFrameworksPlayoff &quot; />
  18. 19. <ul><li>Supported out of the box: </li></ul><ul><ul><li>JSP / JSTL </li></ul></ul><ul><ul><li>XML / XSLT </li></ul></ul><ul><ul><li>Apache Velocity </li></ul></ul><ul><ul><li>Freemarker </li></ul></ul><ul><ul><li>Adobe PDF </li></ul></ul><ul><ul><li>Microsoft Excel </li></ul></ul><ul><ul><li>Jasper Reports </li></ul></ul>Features:: View Technology
  19. 20. Configure the view technology <ul><li>View Resolvers </li></ul><ul><ul><li>Renders the model </li></ul></ul><ul><ul><li>Decoupling the view technology </li></ul></ul><ul><li>JSTL view </li></ul><ul><ul><li>JSP + JSTL –native choice for Spring MVC </li></ul></ul><ul><ul><li>Spring tag libraries </li></ul></ul><bean class = &quot;org.springframework.web.servlet.view.InternalResourceViewResolver&quot; > <property name = &quot;viewClass&quot; value = &quot;org.springframework.web.servlet.view. JstlView “ /> <property name = &quot;prefix&quot; value = &quot;/WEB-INF/views&quot; /> <property name = &quot;suffix&quot; value = &quot; . jsp&quot; /> </bean>
  20. 21. Features:: Page Flow mv.setView( new RedirectView( &quot;../phoneBook“ , true ));
  21. 22. Features:: Page Flow – Step 1 <ul><li>Mapping URLs to handler methods </li></ul><ul><ul><li>Method level annotations </li></ul></ul><ul><ul><li>Can restrict to specific request method </li></ul></ul><ul><ul><li>URL strings can be error prone ! </li></ul></ul>@RequestMapping ( value = &quot;/phoneBook&quot; , method = RequestMethod. GET ) protected ModelAndView setupForm() throws Exception { ModelAndView mv = new ModelAndView(); mv.addObject( &quot;contacts&quot; , Collections.<Contact>emptyList()); mv.addObject( &quot;contact&quot; , new Contact()); … } PhoneBookController.java
  22. 23. Page Flow – Step 1 Contd. DispatcherServlet PhoneBookController Handle GET /demo/phoneBook 1 InternalResource ViewResolver 2 phoneBook /WEB-INF/views/phoneBook.jsp phoneBook
  23. 24. Page Flow – Step 2 <ul><li>Choosing the view to be rendered </li></ul><ul><ul><li>Set the ModelAndView view name </li></ul></ul><ul><ul><li>Or just return a view name String </li></ul></ul>PhoneBookController.java <ul><li>@RequestMapping ( value = &quot;/phoneBook&quot; , method = RequestMethod. GET ) </li></ul><ul><li>protected ModelAndView setupForm() throws Exception { </li></ul><ul><ul><li>ModelAndView mv = new ModelAndView(); </li></ul></ul><ul><ul><li>mv.setViewName( &quot;phoneBook&quot; ); </li></ul></ul><ul><ul><li>return mv; </li></ul></ul>@RequestMapping ( value = &quot;/phoneBook&quot; , method = RequestMethod. GET ) protected ModelAndView setupForm() throws Exception { return &quot; phoneBook &quot; ; }
  24. 25. Page Flow – Step 2 Contd. DispatcherServlet PhoneBookController Handle GET /demo/phoneBook 1 InternalResource ViewResolver 2 phoneBook /WEB-INF/views/phoneBook.jsp phoneBook
  25. 26. Features:: Sorting & Pagination Search results pagination Sorting by column
  26. 27. Features:: Table Sorting <ul><li>Used the display tag library </li></ul><ul><ul><li>Handles column display, sorting, paging, cropping, grouping, exporting, and more </li></ul></ul><ul><ul><li>Supports internal and external sorting </li></ul></ul><ul><ul><li>Sort only visible records or the entire list </li></ul></ul><%@ taglib uri = &quot; http://displaytag.sf.net &quot; prefix = &quot; display &quot; %> <display:table name = &quot;contacts&quot; class = &quot;grid&quot; id = &quot;contacts&quot; sort = &quot;list&quot; pagesize = &quot;5&quot; requestURI = &quot;/demo/phoneBook/list&quot; > <display:column property =&quot; fullName&quot; title = &quot;Name&quot; class = &quot;grid&quot; headerClass = &quot;grid&quot; sortable = &quot;true&quot; /> … </display:table> phoneBook.jsp
  27. 28. Features:: Search Results Pagination <ul><li>Used the display tag pagination support for the demo </li></ul><ul><ul><li>Supports internal and external pagination </li></ul></ul><ul><ul><li>Adds pagination parameter to request </li></ul></ul><ul><ul><li>Limited to HTTP GET </li></ul></ul>
  28. 29. Features:: Form Binding <ul><li>@ModelAttribute annotations </li></ul><ul><li>Spring’s form tag library </li></ul><ul><ul><li>EL expressions for binding path </li></ul></ul><td class = &quot;searchLabel&quot; ><b><label for = &quot;email&quot; > Email </label></b></td> <td class = &quot;search&quot; > <form:input id = &quot;email“ path = &quot;email&quot; tabindex = &quot;2&quot; /> </td> @RequestMapping ( value = &quot;/phoneBook/list&quot; ) protected ModelAndView onSubmit( @ModelAttribute ( &quot;contact&quot; ) Contact contact, BindingResult result)
  29. 30. Features:: Validations <ul><li>Used Spring Modules bean validation framework for server side validation </li></ul><ul><ul><li>Clear separation of concerns </li></ul></ul><ul><ul><li>Declarative validation as in commons-validation </li></ul></ul><ul><ul><li>Supports Valang - extensible expression language for validation rules </li></ul></ul>
  30. 31. Bean Validation Configuration <ul><li>Load validation XML configuration file [1] </li></ul><ul><li>Create a bean validator [2] </li></ul><bean id = &quot;configurationLoader&quot; class = &quot;DefaultXmlBeanValidationConfigurationLoader&quot; > <property name = &quot;resource&quot; value = &quot;WEB-INF/validation.xml&quot; /> </bean> <bean id = &quot;beanValidator&quot; class = &quot;org.springmodules.validation.bean.BeanValidator&quot; > <property name = &quot;configurationLoader&quot; ref = &quot;configurationLoader&quot; /> </bean> 1 2
  31. 32. Bean Validation Configuration <ul><li>Contact email validation </li></ul><ul><ul><li>Bound to the domain model and not to a specific form </li></ul></ul>Validation.xml <validation> <class name = &quot;com.alphacsp.webFrameworksPlayoff . Contact &quot; > <property name = &quot;email&quot; > <email message = &quot;Please enter a valid email address&quot; apply-if = &quot;email IS NOT BLANK&quot; /> </property> </class> </validation> Domain Model
  32. 33. Server Side Validations <ul><li>Validator is injected to the controller </li></ul><ul><li>Reusing the binding errors object </li></ul>PhoneBookController.java @Autowired Validator validator; @RequestMapping ( value = &quot;/phoneBook/list&quot; ) protected ModelAndView onSubmit( @ModelAttribute ( &quot;contact&quot; ) Contact contact, BindingResult result) throws Exception { … validator.validate(contact, result); …
  33. 34. Client Side Validation <ul><li>Valang validator </li></ul><ul><ul><li>validation is defined in valang expression language </li></ul></ul><bean id = &quot;clientSideValidator&quot; class = &quot;org.springmodules.validation.valang.ValangValidator&quot; > <property name = &quot;valang&quot; > <value> <![CDATA[ { firstName : ? IS NOT BLANK OR department IS NOT BLANK OR email IS NOT BLANK : 'At least one field is required'} ]]> </value> </property> </bean>
  34. 35. Client Side Validation Contd. <ul><li>Use Valang tag library to apply the valang validator at client side </li></ul><ul><ul><li>Validation expression is translated to JavaScript </li></ul></ul><script type = &quot;text/javascript&quot; id = &quot;contactValangValidator&quot; > new ValangValidator( 'contact' , true,new Array( new ValangValidator.Rule(' firstName' , 'not implemented' , 'At least one field is required' , function () { return ((! this.isBlank((this.getPropertyValue( 'firstName' )), ( null ))) || (! this.isBlank((this.getPropertyValue( 'department' )), ( null )))) || (! this.isBlank((this.getPropertyValue( 'email' )), ( null )))}))) </script> phoneBook.jsp <%@ taglib uri = &quot;http://www.springmodules.org/tags/valang&quot; prefix = &quot;valang&quot; %> <script type = &quot;text/javascript&quot; src = &quot;/scripts/valang_codebase.js&quot; ></script> <form:form method = &quot;POST&quot; action = &quot;/demo/phoneBook/list&quot; id = &quot;contact&quot; name = &quot;contact&quot; commandName = &quot;contact&quot; > <valang:validate commandName = &quot;contact&quot; />
  35. 36. Features:: AJAX <ul><li>No built in AJAX support </li></ul><ul><ul><li>DWR is unofficially recommended by Spring </li></ul></ul><ul><li>Used DWR to expose the department service to JavaScript </li></ul><ul><ul><li>Requires dealing for low-level AJAX </li></ul></ul><ul><ul><li>DWR has simple integration with Spring </li></ul></ul><ul><li>Used script.aculo.us autocomplete component with DWR </li></ul>
  36. 37. AJAX:: Configuration <ul><li>The department service Spring bean </li></ul><dwr> <allow> <create creator = &quot;spring&quot; javascript = &quot;DepartmentServiceFacade&quot; > <param name = &quot;beanName&quot; value = &quot;departmentServiceFacade&quot; /> </create> </allow> </dwr> <bean id =&quot;departmentServiceFacade&quot; class = &quot;com.alphacsp.webFrameworksPlayoff.service.impl. MockRemoteDepartmentServiceImpl“ /> <ul><li>Exposing it to JavaScript using DWR </li></ul>DWR.xml
  37. 38. AJAX:: Autocomplete Component <script type = &quot;text/javascript&quot; src = &quot;/scripts/prototype/prototype.js&quot; ></script> <script type = &quot;text/javascript&quot; src = &quot;/scripts/script.aculo.us/controls.js&quot; ></script> <script type = &quot;text/javascript&quot; src = &quot;/scripts/autocomplete.js&quot; ></script> <td class=&quot;search&quot;> <form:input id = &quot;department&quot; path = &quot;department&quot; tabindex = &quot;3&quot; cssClass = &quot;searchField&quot; /> <div id = &quot;departmentList&quot; class = &quot;auto_complete&quot; ></div> <script type = &quot;text/javascript&quot; > new Autocompleter.DWR( 'department' , 'departmentList' , updateList, {valueSelector: nameValueSelector, partialChars: 0 }); </script> </td> phoneBook.jsp
  38. 39. Features:: Error Handling <ul><li>HandlerExceptionResolvers - handle unexpected exceptions </li></ul><ul><ul><li>Programmatic exception handling </li></ul></ul><ul><ul><li>Information about what handler was executing when the exception was thrown </li></ul></ul><ul><ul><li>SimpleMappingExceptionResolver – map exception classes to views </li></ul></ul>
  39. 40. Features:: I18n <ul><li>LocaleResolver </li></ul><ul><ul><li>automatically resolve messages using the client's locale </li></ul></ul><ul><ul><ul><li>AcceptHeaderLocaleResolver </li></ul></ul></ul><ul><ul><ul><li>CookieLocaleResolver </li></ul></ul></ul><ul><ul><ul><li>SessionLocaleResolver </li></ul></ul></ul><ul><li>LocaleChangeInterceptor </li></ul><ul><ul><li>change the locale in specific cases </li></ul></ul><ul><li>Reloadable resource bundles </li></ul>
  40. 41. Features:: Documentation <ul><li>Excellent reference documentation ! </li></ul><ul><li>Books – mostly on the entire framework </li></ul><ul><li>Code samples </li></ul><ul><li>Many AppFuse QuickStart applications </li></ul>
  41. 42. Summary <ul><li>Introduction </li></ul><ul><li>Background </li></ul><ul><li>Features Review </li></ul><ul><ul><li>Configuration </li></ul></ul><ul><ul><li>View technology </li></ul></ul><ul><ul><li>Page flow </li></ul></ul><ul><ul><li>Table sorting </li></ul></ul><ul><ul><li>Search results pagination </li></ul></ul><ul><ul><li>Validation </li></ul></ul><ul><ul><li>AJAX </li></ul></ul><ul><ul><li>Error handling </li></ul></ul><ul><ul><li>I18n </li></ul></ul><ul><ul><li>Documentation </li></ul></ul><ul><li>Summary </li></ul>
  42. 43. Summary:: Pros <ul><li>Pros: </li></ul><ul><ul><li>Highly flexible </li></ul></ul><ul><ul><li>Strong REST foundations </li></ul></ul><ul><ul><li>A wide choice of view technologies </li></ul></ul><ul><ul><li>New annotation configuration, less XML, more defaults </li></ul></ul><ul><ul><li>Integrates with many common web solutions </li></ul></ul><ul><ul><li>Easy adoption for Struts 1 users </li></ul></ul>
  43. 44. Summary:: Cons <ul><li>Cons: </li></ul><ul><ul><li>Model2 MVC forces you to build your application around request/response principles as dictated by HTTP </li></ul></ul><ul><ul><ul><li>This was done by design in Spring MVC </li></ul></ul></ul><ul><ul><li>Requires a lot of work in the presentation layer: JSP, Javascript, etc. </li></ul></ul><ul><ul><li>No AJAX support out of the box </li></ul></ul><ul><ul><li>No components support </li></ul></ul>
  44. 45. Summary:: Roadmap <ul><li>Spring 3.0 – August/September 2008 </li></ul><ul><ul><li>Unification in the programming model between Spring Web Flow and Spring MVC </li></ul></ul><ul><ul><ul><li>SpringFaces - JSF Integration </li></ul></ul></ul><ul><ul><ul><li>Spring JavaScript - abstraction over common JavaScript toolkits </li></ul></ul></ul><ul><ul><ul><li>AJAX support </li></ul></ul></ul><ul><ul><ul><li>Conversational state </li></ul></ul></ul>
  45. 46. Summary:: References <ul><li>http://springframework.org/ </li></ul><ul><li>Spring Source </li></ul><ul><li>Spring Modules </li></ul><ul><li>Spring IDE </li></ul><ul><li>Appfuse light - spring MVC quickstarts </li></ul><ul><li>Matt Raible - Spring MVC </li></ul><ul><li>Expert Spring MVC and Web Flow </li></ul>
  46. 47. <ul><li>Thank </li></ul><ul><li>You ! </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.