Tuesday, August 19, 2008

Integrating DWR 2.0.5 with Spring 2.5.5 and web flow 2.0.3

Overview
DWR is an open source Remote Procedure Call (RPC) library that makes it easy to call Java functions from JavaScript and to call JavaScript functions from Java via Reverse Ajax functionality that is introduced in DWR 2.0 version. This article is limited to explaining as to how to configure DWR 2.0.5 using XML namespace support available in Spring 2.0 version and above.

Detailed Steps
  • Step 1 - Add dwr to web.xml file

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">

......

<!-- DWR 2.0.5 Integration -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>

<!-- Turn debug to false in production-->
<init-param>
<param-name>debug</param-name>
<param-value>false</param-value>
</init-param>

<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

</web-app>

  • Step 2 - Add the dwr namespace and schema to your spring configuration file. The namespace is xmlns:dwr=”http://www.directwebremoting.org/schema/spring-dwr” The schemaLocation is http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd. Also map engine.js, util.js, call, dwr and interface request URIS to dwrcontroller (see example below)
Example: Sample Spring MVC configuraton (webmvc-config.xml)


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
xsi:schemaLocation=" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.directwebremoting.org/schema/spring-dwr
http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd">

<!-- Maps request URIs to controllers -->
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<value>
/engine.js=dwrController
/util.js=dwrController
/call/**=dwrController
/dwr/**"=dwrController
/interface/**=dwrController
</value>
</property>
<property name="defaultHandler">
<!-- Selects view names to render based on the request URI: e.g. /main selects "main" -->
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController" />
</property>
<property name="order" value="0"/>
</bean>

.....

<!-- Enable DWR AJAX functionality -->
<dwr:configuration>
<dwr:create javascript="TestData" type="spring" class="com.web.ajax.TestData"/>
<dwr:convert type="bean" class="com.data.ClassLoadingData" />
</dwr:configuration>

<dwr:controller id="dwrController" debug="true" />

<!-- configure spring creators -->
<bean id="testData" class="com.web.ajax.TestData">
<dwr:remote javascript="TestData">
<dwr:include method="refreshClassLoadingData" />
</dwr:remote>
<property name="jvmDataManager" ref="jvmDataManager"/>
</bean>

<bean id="jvmDataManager" class="com.web.jvm.impl.JvmDataManagerImpl/>



  • Step 3 Configure Spring creators - In the above configuration, we expose it directly as spring beans (e.g. testData ).

    The dwr:remote element is the element that will expose the bean to dwr. This is the best approach as it allows you to wire your dependencies like any other spring object.

    Secondly you can configure them in a dwr:configuration much in the same as the standard dwr.xml file works.

  • Step 4 Configure Spring converters - The spring converters must be configured inside of a dwr:configuration element in spring. Please refer the configuration above.

  • Other important elements to know:
  1. dwr:include : Used to include specific methods. Can be used with dwr:convert, dwr:create, or dwr:remote element
  2. dwr:exclude : Used to exclude specific methods. Can be used with dwr:convert, dwr:create, or dwr:remote element
  3. dwr:init : Optional element that will allow you to specify custom creator types and converter types.
  4. dwr:converter : Specifies a custom converter type, must be used within dwr:init.
  5. dwr:create : Specifies a custom creator type, must be used within a dwr:init.
  6. dwr:signature : Used to specify signatures much like the element in the standard dwr.xml.

NOTE: Please make sure you source TestData.js in the page as in the example below.

<script language='JavaScript' type='text/javascript' src='<root-context>/dwr/interface/TestData.js'/>

Replace <root-context> with your app's root context.

Example: AJAX Javascript function refreshClassLoadingData()

function refreshClassLoadingData(){
TestData.refreshClassLoadingData(function(data){
if (data != null){
DWRUtil.setValue('currentLoadedClasses', data.loadedClassCount);
}
});
}
//Please note the "data" represent the ClassLoadingData returned from AJAX function. So
//it is possible to access the variables of ClassLoadingData object in Javascript since
//it is converted as a bean in Javascript.

Example: TestData.java

public class TestData{
@Autowired
@Qualifier("jvmDataManager")
private IjvmDataManager jvmDataManager;

/**
* This is the AJAX function that can be called from UI via DWR AJAX
*/
public ClassLoadingData refreshClassLoadingData(){
return this.jvmDataManager.getClassLoadingData();
}
}


Conclusion
The DWR namespace support in spring configuration eliminates the need of having dwr.xml file placed inside WEB-INF folder. The sample configuration shown above is sufficient enough to get the DWR working with Spring 2.5.5 and spring webflow 2.0.3. Please note the TestData, ClassLoadingData shown above are all plain old java objects.

More Information on DWR is available here.

15 comments:

yAnG said...

Great post! I am new to DWR and Spring integration, I would greatly appreciate if you can share the full source code for the working example? Thanks!

Anonymous said...

Yeah, really appricated if you provide a sample source code.

Unknown said...

Great Post, will give it a try now,can you share some code or a sample application if you have one.Was looking all over web and only find a couple of articles related to dwr and spring web flow 2.0.
Thanks for the post though

Vigil Bose said...

I will come up with a working sample. Please bear with me. I just saw this demand coming.

Ashish said...

Hi. It would be really great if you can share the source code for the spring MVC intrgration with DWR.

Vigil Bose said...

Ashish,

There is no difference in setting up DWR with Spring MVC and Spring Web Flow. You could follow exactly the same way as I posted here in this article for Spring MVC. The source code is also posted in this article.

Regards,
Vigil

Anonymous said...

You have really great taste on catch article titles, even when you are not interested in this topic you push to read it

Anonymous said...

prozac pharmacy http://drugstore4.com/fr/product/imitrex.html superior health pharmacy [url=http://drugstore4.com/de/product/tenormin.html]tenormin[/url]
pharmacy technician colleges in southern illinois http://drugstore4.com/de/product/allopurinol-zyloprim.html us pharmacy officer and responsibility [url=http://drugstore4.com/product/amoxicilline.html]pharmacy law washington state[/url]

Anonymous said...

free lesbin porn nude female wrestling free videos free full length amateurs videos free xx movie free adult amateur photos gay support blue light free live nude free mobile lesbian clip free onion booty porn

Anonymous said...

british virgin islands air travel http://khgf.info/anal/anal-dryhumping anal dryhumping [url=http://khgf.info/adult/adult-and-tube]adult and tube[/url]
http://khgf.info/amateur/canadian-amateur-boxing canadian amateur boxing eric clapton derek trucks crossroads [url=http://khgf.info/condom/o-mercado-administradoras-de-condom-nios]o mercado administradoras de condom nios[/url]
[url=http://khgf.info/bdsm/role-of-rules-in-bdsm]role of rules in bdsm[/url] the gathering lifetime movie http://khgf.info/blow/blow-job-whole-sale blow job whole sale
omega travel okinawa http://khgf.info/porno/big-tit-anal-blacks-porno big tit anal blacks porno [url=http://khgf.info/ass/alicia-keys-ass-pics]alicia keys ass pics[/url]
http://khgf.info/lubricant/norway-lubricant-demand norway lubricant demand the prince and me sound track [url=http://khgf.info/ass/ali-bastian-ass-photos]ali bastian ass photos[/url]

Anonymous said...

coureur de bois travel kit http://hfad.info/teen/free-teen-blonde-sex-movie free teen blonde sex movie [url=http://hfad.info/pissing/girl-pissing-underwear]girl pissing underwear[/url]
http://hfad.info/prostitute/staten-island-prostitute staten island prostitute cheap exterior doors [url=http://hfad.info/virgins/real-virgins-fucked-first-time]real virgins fucked first time[/url]
[url=http://hfad.info/sexual/person-having-sexual-relation]person having sexual relation[/url] sublime directory jennifer love hewitt movie http://hfad.info/thong/linda-kozlowski-crocodile-dundee-thong linda kozlowski crocodile dundee thong
singapore travel agency tour packages http://hfad.info/sexy/hot-sexy-stripping-brunette hot sexy stripping brunette [url=http://hfad.info/nylon/men-and-women-nylon-encasement]men and women nylon encasement[/url]
http://hfad.info/shemale/vyctoria-russian-shemale vyctoria russian shemale south park mexican music [url=http://hfad.info/thongs/hot-babes-in-tiny-string-thongs]hot babes in tiny string thongs[/url]

Mary said...

Hello, How do I get in touch with you? There is no email or contact info listed .. please advise .. thanks .. Mary. Please contact me maryregency at gmail dot com

samia said...

Great post, very informative. Thanks!

Kylie Wilson said...

Good Post

Ella Mapple said...

Intresting Article