我的漫漫程序之旅

专注于JavaWeb开发
随笔 - 39, 文章 - 310, 评论 - 411, 引用 - 0
数据加载中……

小试Annotation来配置DWR

DWR2.0以上版本支持通过配置Annotation的方式来配置DWR,
可以完全抛弃dwr.xml.

1.和用dwr.xml的配置稍稍有一些不一样。来看看:

<?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>

注意对于内部类的语法标识,要用$符号 。例如
java.util.Map$Entry 而不是 java.util.Map.Entry

2.来看一下远程访问类怎么配置:
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();
    }

}


3.再来看看Bean的转换:
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()方法没法调用。

如果你想通过Spring注入来配置DWR只需加入:
@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>

creator :默认就是NewCreate了。

ok,基本完成,这样是不是方便多了。~

posted on 2008-05-30 11:02 々上善若水々 阅读(2078) 评论(2)  编辑  收藏 所属分类: AJAX

评论

# re: 小试Annotation来配置DWR[未登录]  回复  更多评论   

不错,正在找这方面的内容。
2008-05-30 17:44 | 草儿

# re: 小试Annotation来配置DWR[未登录]  回复  更多评论   

哈哈,还能这么用。见识了!
2008-05-30 17:54 |

只有注册用户登录后才能发表评论。


网站导航: