在sdk5.0之后,大部分开源框架都采用了annotation的编程,在dwr中使用annotation能很好地配合dwr.xml的使用。
使用之前必须先配置web.xml 。
1 web.xml配置:
<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> 用逗号割开
2 在远程方法中可以如下使用:
@RemoteProxy public class RemoteFunctions { @RemoteMethod public int calculateFoo() { return 42; } }
@DataTransferObject public class Foo { @RemoteProperty private int foo; public int getFoo() { return foo; } @RemoteProperty public int getBar() { return foo * 42; } }
@RemoteMethod表明了可调用的方法,@RemoteProperty表示可使用的属性