DWR+Struts结合的使用

51 篇文章 0 订阅
36 篇文章 0 订阅

应用DWR+Struts要注意以下两点:

1.其中<load-on-startup>的部分要特別注意,ActionServlet要首先初始化,也就是说数字要比DWR的要小.

2.在struts-config.xml中action标签中的scope属性要设置成session,不可以使用默认的request,其原因是:

【在dwr的org.directwebremoting.struts.StrutsCreator中它是这样获取formbean的:
 ActionForm formInstance = (ActionForm) WebContextFactory.get().getSession().getAttribute(formBean);】

 

在dwr.xml中设置:

<create creator="struts" javascript="JDwrstruts">
   <param name="formBean" value="dwrForm" />
</create>

注:蓝色为固定属性,红色为struts-config.xml中的formbean的name属性值,来对应你的ActionForm.

 

页面代码:

<head>
  <title>JSP for DwrForm form</title>
  <script src="dwr/interface/JDwrstruts.js"></script>
  <script src="dwr/engine.js"></script>
  <script src="dwr/util.js"></script>
  <script type="text/javascript">
   function testdwrstruts(){
    var uname = DWR.Util.getValue('username');
    alert(uname);
    if(uname==' '){
     // 发送请求到服务器,判断用户名是否存在
     JDwrstruts.getUsername(judgeResult);
    }
   }
   
   function judgeResult(data){
    DWR.Util.setValue('username', data);
   }
  </script>
  
 </head>
 <body>
  <html:form action="/dwrStruts.do">
   username:<html:text property="username" οnblur="testdwrstruts();"></html:text>
   <html:submit/><html:cancel/>
  </html:form>
 </body>

注:取控件值时请使用蓝色处获取,否则容易造成控件失效!

 

这里使用的是DWR调用Struts中的formbean,如果想调用Struts中的Action,建议直接调用,不需要使用DWR,Action中构建是业务逻辑,如果非要调用,建议提取业务方法,在Action中自定义方法调用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值