Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Spring and DWR

12,863 views

Published on

Published in: Technology, Education

Spring and DWR

  1. 1. Spring and DWR Frameworks for Rich Web Enterprise Application Thomas Wiradikusuma [email_address] www.wiradikusuma.com Presentation to the 20 th Java User Group Meet Up (JaMU), May 20, 2006
  2. 2. Objective <ul><li>To introduce Spring, an application framework for developing enterprise applications. </li></ul><ul><li>To introduce DWR, an Ajax framework for developing Rich Internet Application (RIA). </li></ul><ul><li>To show Spring+DWR integration to get the best of both worlds. </li></ul>
  3. 3. Agenda <ul><li>Intro to Spring </li></ul><ul><li>Spring wiring </li></ul><ul><li>Spring step-by-step </li></ul><ul><li>Intro to DWR </li></ul><ul><li>DWR configuration </li></ul><ul><li>DWR step-by-step </li></ul><ul><li>Spring and DWR integration </li></ul>
  4. 4. Typical JavaEE App Development <ul><li>Usually developing web applications that serve many users and access (possibly many, heterogeneous) enterprise resources. </li></ul><ul><li>EJB is used because we want some of its features. </li></ul><ul><li>EJB is complicated, for a reason. </li></ul><ul><li>Testing is tedious. </li></ul><ul><li>Is there an easier way? </li></ul>
  5. 5. Here Comes Spring! <ul><li>An open source Java/JavaEE application framework. </li></ul><ul><li>Codes originally came from Rod Johnson’s book “Expert One-on-One: J2EE Design and Development”, Wrox Press, 2002. </li></ul><ul><li>Enables POJOs to achieve things that were previously only possible with EJBs. </li></ul>
  6. 6. Spring Mission Statement <ul><li>JavaEE should be easier to use. </li></ul><ul><li>OO design is more important than any implementation technology, such as JavaEE. </li></ul><ul><li>Testability is essential, and a framework such as Spring should help make your code easier to test. </li></ul><ul><li>Spring should not compete with good existing solutions (Hibernate, Struts, etc), but should foster integration. </li></ul>
  7. 7. Spring Described <ul><li>Lightweight </li></ul><ul><li>Inversion of control </li></ul><ul><li>Aspect-oriented </li></ul><ul><li>Container </li></ul><ul><li>Framework </li></ul>
  8. 8. Spring Modules
  9. 9. Some Spring Usage Scenarios
  10. 10. Some Spring Usage Scenarios - 2
  11. 11. Inversion of Control <ul><li>Dependency injection </li></ul><ul><ul><li>Beans define their dependencies through constructor arguments or properties </li></ul></ul><ul><ul><li>The container provides the injection at runtime </li></ul></ul><ul><li>“ Don’t talk to strangers,” also known as the Hollywood principle, “Don’t call me I’ll call you.” </li></ul><ul><li>Decouples object creators and locators from application logic </li></ul><ul><li>Easy to maintain and reuse </li></ul><ul><li>Testing is easier </li></ul>
  12. 12. Spring Bean Definition <ul><li>The bean class is the actual implementation of the bean being described by the BeanFactory. </li></ul><ul><li>Bean examples: DAO, DataSource, Transaction Manager, Persistence Manager, Service objects, etc. </li></ul><ul><li>Spring config contains implementation classes while your code should program to interfaces. </li></ul><ul><li>Bean behaviors include: </li></ul><ul><ul><li>Singleton or prototype </li></ul></ul><ul><ul><li>Autowiring </li></ul></ul><ul><ul><li>Spring-aware interfaces </li></ul></ul><ul><ul><li>Initialization and destruction methods </li></ul></ul><ul><ul><ul><li>init-method </li></ul></ul></ul><ul><ul><ul><li>destroy-method </li></ul></ul></ul><ul><li>Beans can be configured to have property values set. </li></ul><ul><li>Can read simple values, collections, maps, references to other beans, etc. </li></ul>
  13. 13. Simple Spring Bean Example <ul><li><bean id=&quot;orderBean&quot; class=&quot;example.OrderBeanImpl&quot; init-method=&quot;init&quot;> <property name=“maxOrder&quot;>10</property> <property name=&quot;orderDAO&quot;> <ref bean=&quot;orderDAO&quot;/> </property> </li></ul><ul><li></bean> </li></ul><ul><li>public class OrderBeanImpl implements OrderBean { // ... </li></ul><ul><li>public void setMaxOrder(double maxOrder){ this.maxOrder = maxOrder; </li></ul><ul><li>} </li></ul><ul><li>public void setOrderDAO(OrderDAO orderDAO){ this.orderDAO = orderDAO; </li></ul><ul><li>} </li></ul><ul><li>// ... </li></ul><ul><li>} </li></ul>
  14. 14. Spring BeanFactory <ul><li>BeanFactory is core to the Spring framework </li></ul><ul><ul><li>Lightweight container that loads bean definitions and manages your beans. </li></ul></ul><ul><ul><li>Configured declaratively using an XML file, or files, that determine how beans can be referenced and wired together. </li></ul></ul><ul><ul><li>Knows how to serve and manage a singleton or prototype defined bean </li></ul></ul><ul><ul><li>Responsible for lifecycle methods. </li></ul></ul><ul><ul><li>Injects dependencies into defined beans when served </li></ul></ul><ul><li>Avoids the use of singletons and factories </li></ul>
  15. 15. Spring ApplicationContext <ul><li>A Spring ApplicationContext allows you to get access to the objects that are configured in a BeanFactory in a framework manner. </li></ul><ul><li>ApplicationContext extends BeanFactory </li></ul><ul><ul><li>Adds services such as international messaging capabilities. </li></ul></ul><ul><ul><li>Add the ability to load file resources in a generic fashion. </li></ul></ul><ul><li>Several ways to configure a context: </li></ul><ul><ul><li>XMLWebApplicationContext – Configuration for a web application. </li></ul></ul><ul><ul><li>ClassPathXMLApplicationContext – standalone XML application context </li></ul></ul><ul><ul><li>FileSystemXmlApplicationContext </li></ul></ul><ul><li>Allows you to avoid writing Service Locators </li></ul>
  16. 16. XMLWebApplicationContext <ul><li><context-param> </li></ul><ul><li><param-name>contextConfigLocation</param-name> </li></ul><ul><li><param-value> </li></ul><ul><li> /WEB-INF/applicationContext.xml </li></ul><ul><li></param-value> </li></ul><ul><li></context-param> </li></ul><ul><li><listener> </li></ul><ul><li><listener-class> org.springframework.web.context.ContextLoaderListener </li></ul><ul><li></listener-class> </li></ul><ul><li></listener> </li></ul>
  17. 17. Spring MVC
  18. 18. Sample Spring Application <ul><li>DEMO </li></ul><ul><ul><li>ApplicationContext factory = new ClassPathXmlApplicationContext(“config.xml&quot;); </li></ul></ul><ul><li>index.jsp </li></ul><ul><ul><li><%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %> </li></ul></ul><ul><ul><li><c:redirect url=&quot;something.htm&quot;/> </li></ul></ul><ul><li>Spring Bind Tag </li></ul><ul><ul><li><%@taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;%> </li></ul></ul><ul><ul><li><spring:bind path=&quot;command.property&quot;> </li></ul></ul>
  19. 19. Web is All Around <ul><li>History of dynamic web applications </li></ul><ul><li>Web 2.0 and Ajax </li></ul><ul><li>The DWR approach to Ajax </li></ul><ul><li>Tools and widgets </li></ul>
  20. 20. Ajax – Not a Football Team <ul><li>Not one technology, several technologies: </li></ul><ul><ul><li>XHTML and CSS </li></ul></ul><ul><ul><li>Document Object Model (DOM) </li></ul></ul><ul><ul><li>XML (and XSLT) </li></ul></ul><ul><ul><li>XMLHttpRequest (XHR) </li></ul></ul><ul><ul><li>JavaScript </li></ul></ul>
  21. 21. Ajax Approach to Web
  22. 22. Ajax Approach to Web - 2
  23. 23. Issues in Ajax <ul><li>Browser incompatibilities </li></ul><ul><li>Back button and browser history </li></ul><ul><li>Working with XML in JavaScript </li></ul><ul><li>Accessibility </li></ul>
  24. 24. Ajax Frameworks <ul><li>DWR </li></ul><ul><li>Echo2 </li></ul><ul><li>JSON-RPC </li></ul><ul><li>Sajax </li></ul><ul><li>Many more… </li></ul><ul><li>Why DWR? </li></ul><ul><ul><li>Mostly used framework </li></ul></ul><ul><ul><li>Integrates best with Spring </li></ul></ul>
  25. 25. DWR Described <ul><li>Open source Java library </li></ul><ul><li>Allows code in a web browser to use Java functions running on a web server as if it was in the browser </li></ul><ul><li>Two main parts: </li></ul><ul><ul><li>Java Servlet </li></ul></ul><ul><ul><li>JavaScript </li></ul></ul><ul><li>Dynamically generates JavaScript code based on Java classes </li></ul>
  26. 26. Sample DWR Configuration <ul><li>web.xml: </li></ul><ul><li><servlet> </li></ul><ul><li><servlet-name>dwr-invoker</servlet-name> </li></ul><ul><li><servlet-class> </li></ul><ul><li>uk.ltd.getahead.dwr.DWRServlet </li></ul><ul><li></servlet-class> </li></ul><ul><li><init-param> </li></ul><ul><li><param-name>debug</param-name> </li></ul><ul><li><param-value>true</param-value> </li></ul><ul><li></init-param> </li></ul><ul><li></servlet> </li></ul><ul><li><servlet-mapping> </li></ul><ul><li><servlet-name>dwr-invoker</servlet-name> </li></ul><ul><li><url-pattern>/dwr/*</url-pattern> </li></ul><ul><li></servlet-mapping> </li></ul>
  27. 27. Sample DWR Configuration - 2 <ul><li>dwr.xml: </li></ul><ul><li><!DOCTYPE dwr PUBLIC &quot;-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN&quot; &quot;http://www.getahead.ltd.uk/dwr/dwr10.dtd&quot;> </li></ul><ul><li><dwr> </li></ul><ul><li><allow> </li></ul><ul><li><create creator=&quot;new&quot; javascript=&quot;myDate&quot;> </li></ul><ul><li><param name=&quot;class&quot; value=&quot;java.util.Date&quot;/> </li></ul><ul><li></create> </li></ul><ul><li></allow> </li></ul><ul><li></dwr> </li></ul>
  28. 28. DWR Step-by-step <ul><li>Install the DWR JAR file </li></ul><ul><li>Edit the config files </li></ul><ul><li>Go to the following URL: </li></ul><ul><ul><li>http://localhost:8080/ [YOUR-WEBAPP] /dwr/ </li></ul></ul><ul><li>Put this in your JSP: </li></ul><ul><ul><li><script src='/ [YOUR-WEBAPP] / dwr/interface/ [YOUR-SCRIPT] .js '></script> </li></ul></ul><ul><ul><li><script src='/ [YOUR-WEBAPP] / dwr/engine.js '></script> </li></ul></ul><ul><ul><li><script src='/ [YOUR-WEBAPP] / dwr/util.js '></script> </li></ul></ul>
  29. 29. Spring+DWR Integration <ul><li>Check list: </li></ul><ul><ul><li>Use the latest DWR. The Spring creator has changed so it is well worth checking that you have the latest download. </li></ul></ul><ul><ul><li>Make sure DWR and Spring each are working OK in your application. </li></ul></ul><ul><ul><li>Configure DWR to work with Spring. </li></ul></ul><ul><ul><li>Check result in the debug pages: http://localhost:8080/[YOUR-WEBAPP]/dwr/ </li></ul></ul>
  30. 30. Spring+DWR Integration - 2 <ul><li>The SpringCreator: </li></ul><ul><ul><li><create creator=&quot; spring &quot; javascript=&quot;test&quot;> </li></ul></ul><ul><ul><li><param name=&quot; beanName &quot; value=&quot;test&quot;/> </li></ul></ul><ul><ul><li></create> </li></ul></ul><ul><li>Find the Spring configuration: </li></ul><ul><ul><li>ContextLoaderListener </li></ul></ul><ul><ul><ul><li><context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/beans.xml</param-value></context-param> </li></ul></ul></ul><ul><ul><ul><li><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> </li></ul></ul></ul><ul><ul><li>Or by “location*” parameters </li></ul></ul><ul><ul><ul><li><create creator=&quot;spring&quot; javascript=&quot;test&quot;><param name=&quot;beanName&quot; value=&quot;test&quot;/><param name=&quot; location &quot; value=&quot;beans.xml&quot;/></create> </li></ul></ul></ul><ul><ul><li>Or by setting the BeanFactory Directly </li></ul></ul><ul><ul><ul><li>SpringCreator.setOverrideBeanFactory(BeanFactory) </li></ul></ul></ul>
  31. 31. Resources <ul><li>Spring Website: www.springframework.org </li></ul><ul><li>The Spring Reference Manual. </li></ul><ul><li>Walls, Craig and Ryan Breidenbach. Spring in Action . Manning. 2003. </li></ul><ul><li>Johnson, Rod and Jurgen Hoeller. J2EE without EJB . Wrox. 2004. </li></ul><ul><li>DWR Website: http://www.getahead.ltd.uk/dwr </li></ul>

×