小試Annotation來(lái)配置DWR
DWR2.0以上版本支持通過(guò)配置Annotation的方式來(lái)配置DWR,
可以完全拋棄dwr.xml.
1.和用dwr.xml的配置稍稍有一些不一樣。來(lái)看看:
<?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>
注意對(duì)于內(nèi)部類的語(yǔ)法標(biāo)識(shí),要用$符號(hào) 。例如
2.來(lái)看一下遠(yuǎn)程訪問(wèn)類怎么配置:
package com;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;
/*
類注解,其中name是非必須的。不加的時(shí)候就和類名一樣。
*/
@RemoteProxy(name = "test")
public class TestAction
{
/**
* 遠(yuǎn)程調(diào)用的方法都須加上此注解,否則無(wú)法調(diào)用
* @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;
}

// 測(cè)試Bean
@RemoteMethod
public String testName()
{
User user = new User();
user.setUsername("zdw");
return user.getUsername();
}
}
3.再來(lái)看看Bean的轉(zhuǎn)換:
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;
}
}
部署之后,打開(kāi)調(diào)試頁(yè),我們將看到:
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)沒(méi)標(biāo)注釋的devide()方法沒(méi)法調(diào)用。
如果你想通過(guò)Spring注入來(lái)配置DWR只需加入:
@RemoteProxy(name = "test", creator = SpringCreator.class, creatorParams =
{ @Param(name = "beanName", value = "test") })
對(duì)應(yīng):
<create javascript="test" creator="spring">
<!-- 其中name是固定值,value是在xml注入的bean -->
<param name="beanName" value="test" />
</create>
creator :默認(rèn)就是NewCreate了。
ok,基本完成,這樣是不是方便多了。~
可以完全拋棄dwr.xml.
1.和用dwr.xml的配置稍稍有一些不一樣。來(lái)看看:





































注意對(duì)于內(nèi)部類的語(yǔ)法標(biāo)識(shí),要用$符號(hào) 。例如
java.util.Map$Entry
而不是 java.util.Map.Entry
2.來(lái)看一下遠(yuǎn)程訪問(wèn)類怎么配置:



















































3.再來(lái)看看Bean的轉(zhuǎn)換:

































部署之后,打開(kāi)調(diào)試頁(yè),我們將看到:







如果你想通過(guò)Spring注入來(lái)配置DWR只需加入:






creator :默認(rèn)就是NewCreate了。
ok,基本完成,這樣是不是方便多了。~
posted on 2008-05-30 11:02 々上善若水々 閱讀(2111) 評(píng)論(2) 編輯 收藏 所屬分類: AJAX