DWR2.0以上版本支持通過配置Annotation的方式來配置DWR,
可以完全拋棄dwr.xml.
1.和用dwr.xml的配置稍稍有一些不一樣。來看看:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>
org.directwebremoting.servlet.DwrServlet
</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>classes</param-name>
<param-value>
com.TestAction,
com.User
</param-value>
</init-param>
</servlet>

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



<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>

注意對于內部類的語法標識,要用$符號 。例如
java.util.Map$Entry
而不是
java.util.Map.Entry
2.來看一下遠程訪問類怎么配置:
package com;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

/**//*
類注解,其中name是非必須的。不加的時候就和類名一樣。
*/
@RemoteProxy(name = "test")
public class TestAction


{

/** *//**
* 遠程調用的方法都須加上此注解,否則無法調用
* @param a
* @param b
* @return
*/
@RemoteMethod
public int add(int a, int b)

{
return a + b;
}

@RemoteMethod
public int minus(int a, int b)

{
return a - b;
}

@RemoteMethod
public int multiply(int a, int b)

{
return a * b;
}

public int devide(int a, int b)

{
if (b != 0)
return a / b;
return 0;
}

// 測試Bean
@RemoteMethod
public String testName()

{
User user = new User();
user.setUsername("zdw");
return user.getUsername();
}
}

3.再來看看Bean的轉換:
package com;

import org.directwebremoting.annotations.DataTransferObject;
import org.directwebremoting.annotations.RemoteProperty;

@DataTransferObject
public class User


{
private Integer id;
private String username;

@RemoteProperty
public Integer getId()

{
return id;
}

public void setId(Integer id)

{
this.id = id;
}

@RemoteProperty
public String getUsername()

{
return username;
}

public void setUsername(String username)

{
this.username = username;
}
}
部署之后,打開調試頁,我們將看到:
add(1 , 2 ); 3
multiply(4 ,5 ); 20
minus(1 , 10 ); -9
devide( 1, 1);
(Warning: devide() is excluded: Method access is denied by rules in dwr.xml. See below)
testName( ); "zdw"

我們發(fā)現(xiàn)沒標注釋的devide()方法沒法調用。
如果你想通過Spring注入來配置DWR只需加入:
@RemoteProxy(name = "test", creator = SpringCreator.class, creatorParams =


{ @Param(name = "beanName", value = "test") })
對應:
<create javascript="test" creator="spring">
<!-- 其中name是固定值,value是在xml注入的bean -->
<param name="beanName" value="test" />
</create>
creator :默認就是NewCreate了。
ok,基本完成,這樣是不是方便多了。~