`
iloveflower
  • 浏览: 80357 次
社区版块
存档分类
最新评论
  • iloveflower: 呵呵。好好学习。。。。。。。。。。。。
    java 读书
  • Eric.Yan: 看了一点,不过是电子版的……你这一说到提醒我了,还要继续学习哈 ...
    java 读书

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")) 
分享到:
评论

相关推荐

    dwr2.0.5.jar&dwr2.0.6.jar&dwr3.rc1.jar

    在这个场景中,我们关注的是DWR的三个不同版本的JAR文件:dwr2.0.5.jar、dwr2.0.6.jar以及dwr3.rc1.jar。 1. **DWR 2.0.x 版本**: DWR 2.0.x 是DWR的一个较早版本系列,其中2.0.5和2.0.6是两个连续的小版本更新。...

    dwr包.rar dwr.jar engine.js util.js dwr-noncla.jar readme.txt

    JAR File: dwr.jar (1.08Mb) To DWR enable your web-app WAR File: dwr.war (4.62Mb) Demos/Examples of what DWR can do Sources: dwr-3.0.0.116.rc1-src.zip (71Mb) See also the sources from SVN Non-...

    dwr20.dtd

    dwr20.dtd

    dwr20.dtd约束文件

    dwr20.dtd约束文件。 &lt;!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd" &gt;

    dwr3.x demo 实例 例子

    Direct Web Remoting (DWR) 是一个开源的Java库,允许Web应用程序在客户端JavaScript和服务器端Java之间进行双向通信,从而实现Ajax(Asynchronous JavaScript and XML)功能。DWR3.x是DWR的一个版本,提供了许多...

    DWR2.jar + DWR.xml + DWR2.0.dtd + Web.xml

    **DWR.xml** 是DWR的配置文件,用于定制DWR的行为和设置。在这个文件中,你可以定义哪些Java类和方法可以被浏览器访问,设置安全性限制,以及配置转换器和过滤器等。例如,通过`&lt;allow&gt;`标签,你可以指定允许...

    dwr 2.jar dwr 2.jar

    dwr 2.jar dwr 2.jar

    dwr3.0.jar

    dwr3.0.jar

    dwr3.0.jar_ajax

    2. **AJAX支持**:DWR的核心功能之一是简化AJAX(Asynchronous JavaScript and XML)开发,允许开发者直接在JavaScript中调用服务器端的方法,而无需手动处理HTTP请求和响应。 3. **自动类型转换**:DWR能够自动将...

    dwr1. jar dwr1. jar

    dwr 1 jar dwr 1 jar

    dwr3api+DWR文档.pdf

    1. **安装与配置**:如何在你的Java应用服务器上集成DWR,配置DWR的XML配置文件(dwr.xml),以及如何在web.xml中设置过滤器和servlet。 2. **基本概念**:解释了Reverse Ajax、C/sweetalert、Batching和Caching等...

    dwr-3.0.jar

    最新dwr3.0的包,有需要的朋友就下载吧

    DWR3.rar

    "dwr.rar"可能包含了DWR的源码或者库文件,便于开发者查看内部实现或者在项目中集成。"dwr"文件可能是一个示例项目或配置文件,供学习者实践操作。 在实际开发中,掌握DWR3能够让你创建更具交互性的Web应用,提高...

    DWR3.0.jar、DWR.war和DWR2.0中文说明文档

    通过分析这个war文件,开发者可以学习如何配置DWR的XML配置文件(dwr.xml),以及如何创建可从JavaScript调用的Java方法。 3. **DWR2.0中文说明文档**: 这份文档对于理解DWR的工作原理和使用方法至关重要,特别是...

    dwr源码包,dwr.jar包下载

    1、 导入dwr.jar包 2、 在web.xml中配置dwr,如下: &lt;!-- 配置DWR --&gt; &lt;servlet-name&gt;dwr-invoker org.directwebremoting.servlet.DwrServlet &lt;init-param&gt; &lt;param-name&gt;debug&lt;/param-name&gt; ...

    dwr30.dtd的dtd文件

    dwr30.dtd导入myeclipse后补全就出来了

    Struts1.x Spring2.x Hibernate3.x DWR2.x整合工具文档v1.00

    ### Struts1.x、Spring2.x、Hibernate3.x 和 DWR2.x 整合知识点解析 #### 一、Struts1.x与Spring2.x的整合 **1.1 在web.xml中进行Struts和Spring的配置** 为了实现Struts1.x与Spring2.x的无缝集成,首先需要在...

    dwr util.js

    1. **DWR框架基础** DWR的核心理念是使JavaScript能够像操作本地对象一样操作服务器端的对象。它通过一套自动化的处理机制,包括自动序列化和反序列化,使得JavaScript与Java之间的通信变得简单。`util.js`是DWR...

    dwr3.0.0.116源码 part4

    dwr3.0.0.116源码 part4

    dwr.jar/dwr-2.0.5-src.zip/dwr.zip

    1. **DWR.jar**: 这是DWR的核心库文件,包含了运行DWR所需的所有类和资源。当你在项目中引入dwr.jar,你可以使用DWR提供的API来实现Ajax功能。这个jar文件包括了各种Servlet、Filter、JavaScript接口和辅助类,...

Global site tag (gtag.js) - Google Analytics