Posted on 2011-02-15 17:06
圣騎士武 閱讀(4706)
評論(0) 編輯 收藏
DWR提供的注解類型 —@RemoteProxy、@RemoteMethod、@DataTransferObject和...
經常用到的注解主要有:@RemoteProxy、@RemoteMethod、@DataTransferObject和@RemoteProperty。
1. @RemoteProxy和@RemoteMethod
@RemoteMethod對應于原來dwr.xml文件中的create標簽,用于創建DWR所提供的遠程方法;而@RemoteMethod對應于create標簽中的 <include method=”"/>,用來指定所要暴露的方法名稱。我們舉例來說明:
@RemoteProxy(name="bankFunctions")
public class Bank {
@RemoteMethod
public void buy() {
// ...
}
}
從上面可以看出,@RemoteProxy表示這個類將用于遠程方法,而使用@RemoteMethod指定所要暴露的方法,沒有使用@RemoteMethod的方法將不會顯示在客戶端。
上面的注釋使用dwr.xml表示如下:
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="bankFunctions">
<include method="buy" />
</create>
</allow>
</dwr>
如果使用Spring中的DAO活邏輯層則需要進行如下的設置:
// BookDao
@RemoteProxy(creator = SpringCreator.class,
creatorParams = @Param(name = "beanName", value = "bookDao"),
name="bookFunctions")
public class BookDao {
@RemoteMethod
public void addBook(Book book) {
// ...
}
}
通過指定@RemoteProxy中的creator類型為SpringCreator,然后在creatorParams指定對應的beanName名稱。對應的dwr.xml文件如下:
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="spring" javascript="bookFunctions">
<param name="beanName" value="bookDao" />
<include method="addBook" />
</create>
</allow>
</dwr>
2. @DataTransferObject和@RemoteProperty
@DataTransferObject對應于原來dwr.xml文件中的convert標簽,用于轉換Java對象;@RemoteProperty則對應于convert標簽中的 <param name=”include” value=”" />。
舉例說明一下:
@DataTransferObject
public class Book {
@RemoteProperty
private int id;
@RemoteProperty
private String name;
public Book() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RemoteProperty可以放在JavaBean中的私有變量上面,也可以放在getXXX方法上面。另外如果想將JavaBean中所有的屬性都暴露出來的話,不需要在任何屬性上面添加@RemoteProperty注釋就可以了。
上面的注釋對應的dwr.xml文件如下:
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN"
"http://getahead.ltd.uk/dwr/dwr20.dtd">
<dwr>
<allow>
<convert converter="bean"
match="com.javatang.domain.Book">
<param name="include" value="id, name" />
</convert>
<!-- 或者用下面的方式也可以
<convert converter="bean" match="com.javatang.domain.Book" />
-->
</allow>
</dwr>
關于具體每個注釋使用的方法已經所包含的參數可以參考Java Doc。使用DWR2.0的注解極大的簡化了原來dwr.xml的配置,非常的方便。