用dwr 向后台传递java对象

 例举一个例子:

web.xml文件中的配置

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
 <display-name>anareport</display-name>
 <!-- Desc: DwrServlet Mapping Config -->
 <servlet>  
       <servlet-name>DWRServlet</servlet-name>  
       <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
       <init-param>
               <param-name>debug</param-name>
               <param-value>true</param-value>
       </init-param>
      <init-param>      
              <param-name>crossDomainSessionSecurity</param-name>      
              <param-value>false</param-value>     
       </init-param>
       <init-param>
               <param-name>allowScriptTagRemoting</param-name>
               <param-value>true</param-value>
       </init-param>
 </servlet>  
 <servlet-mapping>   
        <servlet-name>DWRServlet</servlet-name>  
        <url-pattern>/dwr/*</url-pattern>     
 </servlet-mapping>
</web-app>

首先dwr.xml文件中的配置

<dwr>
      <allow>
            <create creator="new" javascript="carCmainUnderWriteList">
                  <param name="class" value="com.sinosoft.anareport.dwr.DWRAction.carUnderWriterListAction" />
            </create>
            <convert match="com.sinosoft.anareport.dwr.DWRAction.Com" converter="bean"  javascript="com"/>  
    </allow>
</dwr>

 

 Com类:

package com.sinosoft.anareport.dwr.DWRAction; //( 为了有说服力 把Com类与carUnderWriterListAction都放在 DWRAction包下)

public class Com {
    private String comCode ;

    public String getComCode() {
         return comCode;
    }

    public void setComCode(String comCode) {
         this.comCode = comCode;
    }
}

dwr实例Action:carUnderWriterListAction如下:

package com.sinosoft.anareport.dwr.DWRAction;

public class carUnderWriterListAction {
     public String str2 (String name, Com com) {
           System.out.println("com.comCode = " + com.getComCode());
           System.out.print("name = " + name);
          return name;
     }
}

 

jsp页面:

<script type='text/javascript' src='/anareport/dwr/interface/carCmainUnderWriteList.js'></script>
<script type='text/javascript' src='/anareport/dwr/engine.js'></script>
<script type='text/javascript' src='/anareport/dwr/util.js'></script>

 <script type="text/javascript">
      function test() {
            var c = new com();  // 在此处就可以直接new  javascript属性的值(在dwr中<convert  ......  javascript="com"/> )
            c.comCode = "06320003"; // 这里就可以设置Com类的属性值
            carCmainUnderWriteList.str2("candy",c,function(data){ // 这里的类Com的实例就可以直接传进去
                  alert("data="+data);
            });
      }
 </script>
  

  <input type=button value="button" οnclick="test();">

  访问jsp页面弹出来的内容为 data=candy,后台打印的com.comCode = 06320003  name = candy

  希望能够帮助一些求知的朋友。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值