step by step guide to DWR 3.RC1 and Sprig 3.0.2 Integration

step by step guide to DWR 3.RC1 and Sprig 3.0.2 Integration


Here are the required steps to integrate dwr and spring-mvc framework. There are some other pages about this topic on the net, but most of them are about previous versions and do not work for dwr3 and spring 3.0.2. Beloware the steps that finally led us to a proper integration.


1- Define a servlet and a servlet mapping in your web.xml to handle dwr requests:

<servlet>
<servlet-name>your-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>your-servlet</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>your-servlet</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>


Note: You do not need to add an extra servlet definition in your web.xml for dwr-invoker, just a mapping for /dwr/* is enough!

2- Define configuration of dwr in your spring-application-context.xml file, this will expose your desired beans as javascript object callable objects at runtime. Here is our spring configuration:

<dwr:controller id="dwrController" debug="true"/>
<dwr:configuration/>
<dwr:annotation-config/>
<dwr:url-mapping/>
<context:component-scan base-package="your.company.dwr.beans"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="order" value="1"/>
</bean>
<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">
<bean class="org.springframework.web.servlet.mvc.UrlFilenameViewController"/>
</property>
<property name="order" value="0"/>
</bean>

Note: you can configure your dwr-related beans in xml, or expose them by annotations, here is xml based configuration(in previous versions dwr.xml was used to do much of these configurations):


<dwr:configuration>
<dwr:create javascript="TestData" type="spring" class="your.company.dwr.beans.TestData"/>
</dwr:configuration>
<bean id="testData" class="your.company.dwr.beans.TestData">
<dwr:remote javascript="TestData">
<dwr:include method="sayHello"/>
</dwr:remote>
</bean>
If you want to use Annotation fr this purpose, just add @RemoteProxy to your component/service class and annotate the methods you like to be exposed to DWR by @RemoteMethod. This will work the same s above bean definition(<dwr:remote> acts like @RemoteMethod and <dwr:create> acts like @RemoteProxy).



3- up until now, you have configured dwr configuration at server-side. Now just call the methods from your pages.

<c:set var="ctx" value="${pageContext.request.contextPath}"/>


<script type='text/javascript' src='${ctx}/dwr/interface/TestData.js'></script>
<script type='text/javascript' src='${ctx}/dwr/interface/DwrSample.js'></script>

<script type="text/javascript">

function testDwr() {
TestData.sayHello('hashem', handleGetData);
DwrSample.sayHello('sadegh', handleGetData);
}

function handleGetData(str) {
alert(str);
}

</script>

4- That's it, no more steps!

*************************************************************
DWR Annotations

DWR annotations can be used as a replacement as well as in conjunction with dwr.xml.

Setup

To use DWR with annotations you need to specify a different DWR controller servlet in your web.xml:

<servlet>
<description>DWR controller servlet</description>
<servlet-name>DWR controller servlet</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>classes</param-name>
<param-value>
com.example.RemoteFunctions,
com.example.RemoteBean
</param-value>
</init-param>
</servlet>
The classes servlet parameter must provide a comma-separated list of the fully-qualified class names of all annotated classes to be used with DWR.

The syntax for inner classes is to use '$' notation (as used by Class.forName()) rather than '.' notation (as used by import statements). For example, use java.util.Map$Entry and not java.util.Map.Entry.

Remote Class Access

To make a simple class available for remote access, use the @RemoteProxy and @RemoteMethod annotations:
@RemoteProxy
public class RemoteFunctions {
@RemoteMethod
public int calculateFoo() {
return 42;
}
}

Any method not annotated with @RemoteMethod will not be available for remote access.

To use a scripting name different from the class name, use the name attribute of @RemoteProxy:
@RemoteProxy(name="Functions")
public class RemoteFunctions {
}

The @RemoteProxy has options available please see the Java docs for more details.

Object Conversion

To make simple bean classes available for remote access, use the @DataTransferObject and @RemoteProperty annotations:
@DataTransferObject
public class Foo {
@RemoteProperty
private int foo;

public int getFoo() {
return foo;
}

@RemoteProperty
public int getBar() {
return foo * 42;
}
}

To use more sophisticated converters see the converter attribute of the @DataTransferObject annotation. Here is an example that configures the Hibernate converter and tells DWR to exclude propertyToExclude from serialization.
// Standard Annotation Configuration
@DataTransferObject(converter=H3BeanConverter.class,
params = @Param(name = "exclude", value = "propertyToExclude"))

Note: If you are using Spring you will need to use the type attribute:
// Spring Annotation Configuration
@DataTransferObject(type="hibernate3",
params = @Param(name = "exclude", value = "propertyToExclude"))
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值