DWR Annotations

时间:2023-03-08 22:04:59

DWR   Annotations

DWR 标注是用来代替 dwr.xml 或者与其一同工作的。

1.初始配置

 <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 定义的时可以标注的类的全名,这些名字用逗号分割。

2.远程访问类

  要使一个简单的 class 可以成为远程访问类,你需要使用@Create 和@RemoteMethod 标注。

 @Create
public class RemoteFunctions {
@RemoteMethod
public int calculateFoo() {
return 42;
}
}

  没有被@RemoteMethod 标注的方法不能被远程访问。

  要在 Javascript 使用不同于类型的名字,使用@Create 标注的 name 属性。

@Create(name = "Functions")
public class RemoteFunctions {
}

3.对象转换

  要使一个简单的 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  属性。

错误和异常处理