DWR通过Annotation与spring整合

本文介绍如何使用DWR 2.0的注解特性与Spring框架整合,实现Java对象的远程调用。通过具体示例展示了配置过程、类及方法的注解使用方式,并提供了完整的HTML页面示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DWR 2.0 增加了一个很有趣的新特性,Annotation,因此可以摆脱了dwr.xml里面的配置.同时也可以方便的和spring整合.

  从官方网站下载dwr.jar包。然后将它放在你webapp的WEB-INF/lib目录下。 修改web.xml

 


< 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 > logLevel </ param-name >
            
< param-value > DEBUG </ param-value >
        
</ init-param >
        
< init-param >
            
< param-name > classes </ param-name >
            
< param-value >
       com.spring.User,com.beans.Book
            
</ param-value >
        
</ init-param >
    
</ servlet >

    
< servlet-mapping >
        
< servlet-name > dwr-invoker </ servlet-name >
        
< url-pattern > /dwr/* </ url-pattern >
    
</ servlet-mapping >

    
< context-param >
        
< param-name > contextConfigLocation </ param-name >
        
< param-value >
            /WEB-INF/beans.xml
        
</ param-value >
    
</ context-param >
    
< listener >
        
< listener-class >
            org.springframework.web.context.ContextLoaderListener
        
</ listener-class >
    
</ listener >

 

       beans.xml是spring的配置文件.org.springframework.web.context.ContextLoaderListener是一个监听器.

<  init-param  >  
             
<  param-name  >  classes  </  param-name  >  
             
<  param-value  >  
       com.spring.User,com.beans.Book
             
</  param-value  >  
         
</  init-param  >  

param-value:参数为被注解的类. 

编写相关类          
  com.beans.Book:

@DataTransferObject(converter=BeanConverter.class)
public   class  Book {
    @RemoteProperty
    
private  String name;
    @RemoteProperty
    
private  String author;

    
public  String getAuthor() {
        
return  author;
    }

    
public   void  setAuthor(String author) {
        
this .author  =  author;
    }

    
public  String getName() {
        
return  name;
    }

    
public   void  setName(String name) {
        
this .name  =  name;
    }
}

        @DataTransferObject: 标注在客户端和服务器之间转换类.对应dwr.xml中的<convert>标签.
               注解的源代码:
                     

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public  @ interface  DataTransferObject
{
    
/**
     * Converter that converts instance of the class (default: bean converter).
     
*/
    Class
<?   extends  Converter >  converter()  default  BeanConverter. class ;

    
/**
     * Parameters for the converter.
     
*/
    Param[] params() 
default  {};
}

         关于annotation可以看这篇文章,java元数据
        @RemoteProperty :标注在类中需要转换的属性.
            源代码:
                  

@Target({ ElementType.FIELD, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public  @ interface  RemoteProperty
{
}


       如果使用dwr.xml配置,可以这样:
      

< convert  converter ="bean"  match ="com.beans.Book" >
  
< param  name ="include"  value ="name, author" />
</ convert >


 User:

@RemoteProxy(name = " user " ,creator = SpringCreator. class ,
        creatorParams
= {
            @Param(name
= " beanName " ,value = " user " )
            })
@DataTransferObject(converter
= BeanConverter. class )
public   class  User {
    @RemoteProperty 
    
private  String welcome;
    @RemoteProperty 
    
private  String username;
    @RemoteProperty 
    
private  String address;
    @RemoteProperty 
    
private  List < Book >  books;
    @RemoteProperty
    
private   int  age;

    
public  String getAddress() {
        
return  address;
    }

    
public   void  setAddress(String address) {
        
this .address  =  address;
    }

    
public   int  getAge() {
        
return  age;
    }

    
public   void  setAge( int  age) {
        
this .age  =  age;
    }
    @RemoteMethod
    
public  String getUsername() {
        
return  username;
    }

    
public   void  setUsername(String username) {
        
this .username  =  username;
    }

    
public  String getWelcome() {
        
return  welcome;
    }

    
public   void  setWelcome(String welcome) {
        
this .welcome  =  welcome;
    }
    @RemoteMethod
    
public  List < Book >  getBooks() {
        
return  books;
    }
    
    
public   void  setBooks(List < Book >  books) {
        
this .books  =  books;
    }
    @RemoteMethod
    
public  User getUser(String welcome) {
        
this .welcome  =  welcome;
        
return   this ;
    }

}

      @RemoteProxy:标注要给远程调用的类.
               RemoteProxy的name设置创造出来的对象的名字,creator指定使用那种创造器,例子中使用SpringCreator.creatorParams指定创造器的其他参数.不同的创造器参数是不同的.
      @RemoteMethod:标注给远程调用的方法

4.修改beans.xml
      

< bean  id ="remote"  class ="com.spring.Remote" ></ bean >
    
< bean  id ="user"  class ="com.spring.User"  
            p:username
="windfree"  p:address ="anhui hefei"  p:age ="25" >
        
< property  name ="books" >
            
< list >
                
< ref  bean ="C" />
                
< ref  bean ="java" />
            
</ list >
        
</ property >
    
</ bean >
    
< bean  id ="java"  class ="com.beans.Book"  p:name ="java"  p:author ="mypure" ></ bean >
    
< bean  id ="C"  class ="com.beans.Book"  p:name ="C"  p:author ="zgliu" ></ bean >

   其中p为spring2.0中提供的标签.
5.html页面
      

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" >
< html >
    
< head >
        
< title > SpringUserInfo.html </ title >
        
< script  type ='text/javascript'  src ='/DWRExample/dwr/interface/user.js' ></ script >
        
< script  type ='text/javascript'  src ='/DWRExample/dwr/engine.js' ></ script >
        
< script  type ='text/javascript'  src ='/DWRExample/dwr/util.js' ></ script >
        
< script  type ="text/javascript" >
        
function  test(){
            user.getUser(
" Hello " ,callback);   
        }
        
var  cellFuncs  =  [
              
function (data) {  return  data.name; },
              
function (data) {  return  data.author; },
            ];
        
function  callback(user){
            
// alert(user.books)
             DWRUtil.setValue('result', " 欢迎你! " + "    姓名: " + user.username + " ,年龄: " + user.age + " ,住址: " + user.address);   
             DWRUtil.addRows('tableInfo',user.books,cellFuncs,
                     { escapeHtml:
false  ,
                   rowCreator:
function (options) {
                        
var  row  =  document.createElement( " tr " );
                        
var  index  =  options.rowIndex  *   50 ;
                        row.style.color 
=   " rgb( "   +  index  +   " ,0,0) " ;
                        
return  row;
                      },
                  cellCreator:
function (options) {
                    
var  td  =  document.createElement( " td " );
                    
var  index  =   255   -  (options.rowIndex  *   50 );
                    td.style.backgroundColor 
=   " rgb( "   +  index  +   " ,255,255) " ;
                    td.style.fontWeight 
=   " bold " ;
                    
return  td;
                  }
             })
        }
    
</ script >


    
</ head >

    
< body >
        
&nbsp;&nbsp;&nbsp;   &nbsp;&nbsp;
        
< input  id ="jbutton"  type ="button"  value ="取得信息"  onclick ="test()"   />
        
< br >
        
< div  id ="result" ></ div >< br >
        
< table  border ="1" >
            
< thead >< tr >
                
< th > 书名 </ th >< th > 姓名 </ th >                 
            
</ tr ></ thead >
            
< tbody  id ="tableInfo" >
            
</ tbody >
        
</ table >
    
</ body >
</ html >

      其中使用了util.js中的一些函数.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值