DWR Annotations

DWR   Annotations

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

1.初始配置

 1 <servlet>
 2     <description>DWR controller servlet</description>
 3     <servlet-name>DWR controller servlet</servlet-name>
 4     <servlet-class>
 5         org.directwebremoting.servlet.DwrServlet
 6     </servlet-class>
 7     <init-param>
 8         <param-name>classes</param-name>
 9         <param-value>
10             com.example.RemoteFunctions, com.example.RemoteBean
11         </param-value>
12     </init-param>
13 </servlet>    

  servlet 参数 classes 定义的时可以标注的类的全名,这些名字用逗号分割。

2.远程访问类

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

1 @Create
2 public class RemoteFunctions {
3     @RemoteMethod
4     public int calculateFoo() {
5         return 42;
6     }
7 }

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

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

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

3.对象转换

  要使一个简单的 bean 类可以被远程访问,  使用@Convert 和@RemoteProperty 标注:

 1 @Convert
 2 public class Foo {
 3 
 4     @RemoteProperty
 5     private int foo;
 6 
 7     public int getFoo() {
 8         return foo;
 9     }
10 
11     @RemoteProperty
12     public int getBar() {
13         return foo * 42;
14     }
15 }

  要使用复杂的转换器,使用@Convert 标注的  converter  属性。

错误和异常处理

 

  

posted @ 2014-04-23 22:43  不苦先生  阅读(112)  评论(0编辑  收藏  举报