浏览 1329 次
锁定老帖子 主题:小试Annotation来配置DWR
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-04-22
最后修改:2010-03-16
DWR2.0以上版本支持通过配置Annotation的方式来配置DWR,
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>classes</param-name> <param-value> com.TestAction, com.User </param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
package com;
import org.directwebremoting.annotations.RemoteMethod; import org.directwebremoting.annotations.RemoteProxy; /* 类注解,其中name是非必须的。不加的时候就和类名一样。 */ @RemoteProxy(name = "test") public class TestAction { /** * 远程调用的方法都须加上此注解,否则无法调用 * @param a * @param b * @return */ @RemoteMethod public int add(int a, int b) { return a + b; } @RemoteMethod public int minus(int a, int b) { return a - b; } @RemoteMethod public int multiply(int a, int b) { return a * b; } public int devide(int a, int b) { if (b != 0) return a / b; return 0; } // 测试Bean @RemoteMethod public String testName() { User user = new User(); user.setUsername("zdw"); return user.getUsername(); } }
package com;
import org.directwebremoting.annotations.DataTransferObject; import org.directwebremoting.annotations.RemoteProperty; @DataTransferObject public class User { private Integer id; private String username; @RemoteProperty public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @RemoteProperty public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
add(1 , 2 ); 3
multiply(4 ,5 ); 20 minus(1 , 10 ); -9 devide( 1, 1); (Warning: devide() is excluded: Method access is denied by rules in dwr.xml. See below) testName( ); "zdw" 我们发现没标注释的devide()方法没法调用。
@RemoteProxy(name = "test", creator = SpringCreator.class, creatorParams =
{ @Param(name = "beanName", value = "test") }) 对应:
<create javascript="test" creator="spring">
<!-- 其中name是固定值,value是在xml注入的bean --> <param name="beanName" value="test" /> </create>
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |