Spring and DWR

50 %
50 %
Information about Spring and DWR

Published on January 24, 2008

Author: wiradikusuma

Source: slideshare.net

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

Objective To introduce Spring, an application framework for developing enterprise applications. To introduce DWR, an Ajax framework for developing Rich Internet Application (RIA). To show Spring+DWR integration to get the best of both worlds.

To introduce Spring, an application framework for developing enterprise applications.

To introduce DWR, an Ajax framework for developing Rich Internet Application (RIA).

To show Spring+DWR integration to get the best of both worlds.

Agenda Intro to Spring Spring wiring Spring step-by-step Intro to DWR DWR configuration DWR step-by-step Spring and DWR integration

Intro to Spring

Spring wiring

Spring step-by-step

Intro to DWR

DWR configuration

DWR step-by-step

Spring and DWR integration

Typical JavaEE App Development Usually developing web applications that serve many users and access (possibly many, heterogeneous) enterprise resources. EJB is used because we want some of its features. EJB is complicated, for a reason. Testing is tedious. Is there an easier way?

Usually developing web applications that serve many users and access (possibly many, heterogeneous) enterprise resources.

EJB is used because we want some of its features.

EJB is complicated, for a reason.

Testing is tedious.

Is there an easier way?

Here Comes Spring! An open source Java/JavaEE application framework. Codes originally came from Rod Johnson’s book “Expert One-on-One: J2EE Design and Development”, Wrox Press, 2002. Enables POJOs to achieve things that were previously only possible with EJBs.

An open source Java/JavaEE application framework.

Codes originally came from Rod Johnson’s book “Expert One-on-One: J2EE Design and Development”, Wrox Press, 2002.

Enables POJOs to achieve things that were previously only possible with EJBs.

Spring Mission Statement JavaEE should be easier to use. OO design is more important than any implementation technology, such as JavaEE. Testability is essential, and a framework such as Spring should help make your code easier to test. Spring should not compete with good existing solutions (Hibernate, Struts, etc), but should foster integration.

JavaEE should be easier to use.

OO design is more important than any implementation technology, such as JavaEE.

Testability is essential, and a framework such as Spring should help make your code easier to test.

Spring should not compete with good existing solutions (Hibernate, Struts, etc), but should foster integration.

Spring Described Lightweight Inversion of control Aspect-oriented Container Framework

Lightweight

Inversion of control

Aspect-oriented

Container

Framework

Spring Modules

Some Spring Usage Scenarios

Some Spring Usage Scenarios - 2

Inversion of Control Dependency injection Beans define their dependencies through constructor arguments or properties The container provides the injection at runtime “ Don’t talk to strangers,” also known as the Hollywood principle, “Don’t call me I’ll call you.” Decouples object creators and locators from application logic Easy to maintain and reuse Testing is easier

Dependency injection

Beans define their dependencies through constructor arguments or properties

The container provides the injection at runtime

“ Don’t talk to strangers,” also known as the Hollywood principle, “Don’t call me I’ll call you.”

Decouples object creators and locators from application logic

Easy to maintain and reuse

Testing is easier

Spring Bean Definition The bean class is the actual implementation of the bean being described by the BeanFactory. Bean examples: DAO, DataSource, Transaction Manager, Persistence Manager, Service objects, etc. Spring config contains implementation classes while your code should program to interfaces. Bean behaviors include: Singleton or prototype Autowiring Spring-aware interfaces Initialization and destruction methods init-method destroy-method Beans can be configured to have property values set. Can read simple values, collections, maps, references to other beans, etc.

The bean class is the actual implementation of the bean being described by the BeanFactory.

Bean examples: DAO, DataSource, Transaction Manager, Persistence Manager, Service objects, etc.

Spring config contains implementation classes while your code should program to interfaces.

Bean behaviors include:

Singleton or prototype

Autowiring

Spring-aware interfaces

Initialization and destruction methods

init-method

destroy-method

Beans can be configured to have property values set.

Can read simple values, collections, maps, references to other beans, etc.

Simple Spring Bean Example <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> </bean> public class OrderBeanImpl implements OrderBean { // ... public void setMaxOrder(double maxOrder){ this.maxOrder = maxOrder; } public void setOrderDAO(OrderDAO orderDAO){ this.orderDAO = orderDAO; } // ... }

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

</bean>

public class OrderBeanImpl implements OrderBean { // ...

public void setMaxOrder(double maxOrder){ this.maxOrder = maxOrder;

}

public void setOrderDAO(OrderDAO orderDAO){ this.orderDAO = orderDAO;

}

// ...

}

Spring BeanFactory BeanFactory is core to the Spring framework Lightweight container that loads bean definitions and manages your beans. Configured declaratively using an XML file, or files, that determine how beans can be referenced and wired together. Knows how to serve and manage a singleton or prototype defined bean Responsible for lifecycle methods. Injects dependencies into defined beans when served Avoids the use of singletons and factories

BeanFactory is core to the Spring framework

Lightweight container that loads bean definitions and manages your beans.

Configured declaratively using an XML file, or files, that determine how beans can be referenced and wired together.

Knows how to serve and manage a singleton or prototype defined bean

Responsible for lifecycle methods.

Injects dependencies into defined beans when served

Avoids the use of singletons and factories

Spring ApplicationContext A Spring ApplicationContext allows you to get access to the objects that are configured in a BeanFactory in a framework manner. ApplicationContext extends BeanFactory Adds services such as international messaging capabilities. Add the ability to load file resources in a generic fashion. Several ways to configure a context: XMLWebApplicationContext – Configuration for a web application. ClassPathXMLApplicationContext – standalone XML application context FileSystemXmlApplicationContext Allows you to avoid writing Service Locators

A Spring ApplicationContext allows you to get access to the objects that are configured in a BeanFactory in a framework manner.

ApplicationContext extends BeanFactory

Adds services such as international messaging capabilities.

Add the ability to load file resources in a generic fashion.

Several ways to configure a context:

XMLWebApplicationContext – Configuration for a web application.

ClassPathXMLApplicationContext – standalone XML application context

FileSystemXmlApplicationContext

Allows you to avoid writing Service Locators

XMLWebApplicationContext <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/applicationContext.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>

/WEB-INF/applicationContext.xml

</param-value>

</context-param>

<listener>

<listener-class> org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

Spring MVC

Sample Spring Application DEMO ApplicationContext factory = new ClassPathXmlApplicationContext(“config.xml&quot;); index.jsp <%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %> <c:redirect url=&quot;something.htm&quot;/> Spring Bind Tag <%@taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;%> <spring:bind path=&quot;command.property&quot;>

DEMO

ApplicationContext factory = new ClassPathXmlApplicationContext(“config.xml&quot;);

index.jsp

<%@ taglib uri=&quot;http://java.sun.com/jsp/jstl/core&quot; prefix=&quot;c&quot; %>

<c:redirect url=&quot;something.htm&quot;/>

Spring Bind Tag

<%@taglib uri=&quot;http://www.springframework.org/tags&quot; prefix=&quot;spring&quot;%>

<spring:bind path=&quot;command.property&quot;>

Web is All Around History of dynamic web applications Web 2.0 and Ajax The DWR approach to Ajax Tools and widgets

History of dynamic web applications

Web 2.0 and Ajax

The DWR approach to Ajax

Tools and widgets

Ajax – Not a Football Team Not one technology, several technologies: XHTML and CSS Document Object Model (DOM) XML (and XSLT) XMLHttpRequest (XHR) JavaScript

Not one technology, several technologies:

XHTML and CSS

Document Object Model (DOM)

XML (and XSLT)

XMLHttpRequest (XHR)

JavaScript

Ajax Approach to Web

Ajax Approach to Web - 2

Issues in Ajax Browser incompatibilities Back button and browser history Working with XML in JavaScript Accessibility

Browser incompatibilities

Back button and browser history

Working with XML in JavaScript

Accessibility

Ajax Frameworks DWR Echo2 JSON-RPC Sajax Many more… Why DWR? Mostly used framework Integrates best with Spring

DWR

Echo2

JSON-RPC

Sajax

Many more…

Why DWR?

Mostly used framework

Integrates best with Spring

DWR Described Open source Java library Allows code in a web browser to use Java functions running on a web server as if it was in the browser Two main parts: Java Servlet JavaScript Dynamically generates JavaScript code based on Java classes

Open source Java library

Allows code in a web browser to use Java functions running on a web server as if it was in the browser

Two main parts:

Java Servlet

JavaScript

Dynamically generates JavaScript code based on Java classes

Sample DWR Configuration web.xml: <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> uk.ltd.getahead.dwr.DWRServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>

web.xml:

<servlet>

<servlet-name>dwr-invoker</servlet-name>

<servlet-class>

uk.ltd.getahead.dwr.DWRServlet

</servlet-class>

<init-param>

<param-name>debug</param-name>

<param-value>true</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>dwr-invoker</servlet-name>

<url-pattern>/dwr/*</url-pattern>

</servlet-mapping>

Sample DWR Configuration - 2 dwr.xml: <!DOCTYPE dwr PUBLIC &quot;-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN&quot; &quot;http://www.getahead.ltd.uk/dwr/dwr10.dtd&quot;> <dwr> <allow> <create creator=&quot;new&quot; javascript=&quot;myDate&quot;> <param name=&quot;class&quot; value=&quot;java.util.Date&quot;/> </create> </allow> </dwr>

dwr.xml:

<!DOCTYPE dwr PUBLIC &quot;-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN&quot; &quot;http://www.getahead.ltd.uk/dwr/dwr10.dtd&quot;>

<dwr>

<allow>

<create creator=&quot;new&quot; javascript=&quot;myDate&quot;>

<param name=&quot;class&quot; value=&quot;java.util.Date&quot;/>

</create>

</allow>

</dwr>

DWR Step-by-step Install the DWR JAR file Edit the config files Go to the following URL: http://localhost:8080/ [YOUR-WEBAPP] /dwr/ Put this in your JSP: <script src='/ [YOUR-WEBAPP] / dwr/interface/ [YOUR-SCRIPT] .js '></script> <script src='/ [YOUR-WEBAPP] / dwr/engine.js '></script> <script src='/ [YOUR-WEBAPP] / dwr/util.js '></script>

Install the DWR JAR file

Edit the config files

Go to the following URL:

http://localhost:8080/ [YOUR-WEBAPP] /dwr/

Put this in your JSP:

<script src='/ [YOUR-WEBAPP] / dwr/interface/ [YOUR-SCRIPT] .js '></script>

<script src='/ [YOUR-WEBAPP] / dwr/engine.js '></script>

<script src='/ [YOUR-WEBAPP] / dwr/util.js '></script>

Spring+DWR Integration Check list: Use the latest DWR. The Spring creator has changed so it is well worth checking that you have the latest download. Make sure DWR and Spring each are working OK in your application. Configure DWR to work with Spring. Check result in the debug pages: http://localhost:8080/[YOUR-WEBAPP]/dwr/

Check list:

Use the latest DWR. The Spring creator has changed so it is well worth checking that you have the latest download.

Make sure DWR and Spring each are working OK in your application.

Configure DWR to work with Spring.

Check result in the debug pages: http://localhost:8080/[YOUR-WEBAPP]/dwr/

Spring+DWR Integration - 2 The SpringCreator: <create creator=&quot; spring &quot; javascript=&quot;test&quot;> <param name=&quot; beanName &quot; value=&quot;test&quot;/> </create> Find the Spring configuration: ContextLoaderListener <context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/beans.xml</param-value></context-param> <listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener> Or by “location*” parameters <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> Or by setting the BeanFactory Directly SpringCreator.setOverrideBeanFactory(BeanFactory)

The SpringCreator:

<create creator=&quot; spring &quot; javascript=&quot;test&quot;>

<param name=&quot; beanName &quot; value=&quot;test&quot;/>

</create>

Find the Spring configuration:

ContextLoaderListener

<context-param><param-name>contextConfigLocation</param-name><param-value>/WEB-INF/classes/beans.xml</param-value></context-param>

<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>

Or by “location*” parameters

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

Or by setting the BeanFactory Directly

SpringCreator.setOverrideBeanFactory(BeanFactory)

Resources Spring Website: www.springframework.org The Spring Reference Manual. Walls, Craig and Ryan Breidenbach. Spring in Action . Manning. 2003. Johnson, Rod and Jurgen Hoeller. J2EE without EJB . Wrox. 2004. DWR Website: http://www.getahead.ltd.uk/dwr

Spring Website: www.springframework.org

The Spring Reference Manual.

Walls, Craig and Ryan Breidenbach. Spring in Action . Manning. 2003.

Johnson, Rod and Jurgen Hoeller. J2EE without EJB . Wrox. 2004.

DWR Website: http://www.getahead.ltd.uk/dwr

Add a comment

Related pages

Spring Integration - DWR

DWR and Spring. Initial Considerations; DWR and Spring step by step - Step 1 - Give DWR access to the Spring context If you are using Spring MVC
Read more

DWR - Easy Ajax for JAVA

DWR provides integration with Spring, Struts, Guice, Hibernate and others. DWR is Open Source, available under the Apache Software License v2.
Read more

Design Within Reach | The Best in Modern Furniture and ...

Design Within Reach offers the world’s largest selection of authentic modern furniture, lighting and accessories from designers past and present
Read more

Spring-Hibernate-DWR - Wiki - Liferay

**Integrating Spring MVC Portlet 3.0 M2, Hibernate 3.3.1 GA, DWR 3 RC1 and Liferay 5.2.2** // **by Ashish Sarin** // <> == INTRODUCTION ...
Read more

dwr+spring集成 - Ruthless - 博客园 - cnblogs.com

DWR(Direct Web Remoting)是一个用于改善web页面与Java类交互的远程服务器端Ajax开源框架,可以帮助开发人员开发包含AJAX ...
Read more

Spring MVC整合DWR - geloin - 博客频道 - CSDN.NET

版权声明:本文为博主原创文章,未经博主允许不得转载。 本文基于Spring MVC整合Mybatis。 (1) 导入包dwr.jar。 (2 ...
Read more

Ajax Form Validation Using Spring and DWR Blog | Oracle ...

Contents Getting Started < li>Server-Side Validation Using Spring Ajax Using DWR Controlling the Process with JavaScript Conclusion < li>Resources ...
Read more

Spring-Hibernate-DWR - Wiki - Liferay.com

Integrating Spring MVC Portlet 3.0 M2, Hibernate 3.3.1 GA, DWR 3 RC1 and Liferay 5.2.2. by Ashish Sarin <> INTRODUCTION# This article ...
Read more

在 Spring Web MVC 环境下使用 DWR - ibm.com

DWR (Direct Web Remoting) 是在 Apache 许可下的开源项目,是一个非常专业的 Ajax 框架。使用 DWR 框架,可以将 Java 组件的 ...
Read more

California Department of Water Resources

The Department of Water Resources (DWR) is responsible for managing and protecting California’s water resources. DWR works with other agencies ...
Read more