DWR Annotations
DWR标注是用来代替dwr.xml或者与其一同工作的。
初始配置
要使用DWR的标注,你需要在web.xml中配置不同的DWR控制器。
<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>
servlet参数classes定义的时可以标注的类的全名,这些名字用逗号分割。
远程访问类
要使一个简单的class可以成为远程访问类,你需要使用 (Create和)RemoteMethod标注。
@Create
public class RemoteFunctions {
@RemoteMethod
public int calculateFoo() {
return 42;
}
}
没有被@RemoteMethod标注的方法不能被远程访问。
要在Javascript使用不同于类型的名字,使用@Create标注的 name 属性。
@Create(name="Functions")
public class RemoteFunctions {
}
对象转换
要使一个简单的bean类可以被远程访问, 使用 (Convert和)RemoteProperty标注:
@Convert
public class Foo {
@RemoteProperty
private int foo;
public int getFoo() {
return foo;
}
@RemoteProperty
public int getBar() {
return foo * 42;
}
}
要使用复杂的转换器,使用@Convert标注的 converter 属性。