联动----续

      前段时间写的关于JSP+servlet+javaBean实现联动的例子,提到存在一个问题就是当再次访问servlet后转回页面后原本页面的数据被刷新掉了。当时我设想如果使用struts实现的话由于他的form的特制,可以避免由于刷新而冲掉数据。最近由于需要对这个进行了实现。
      基本思路:
       首先从页面中利用js获得下拉列表的选中值,然后触发onchange事件,这个事件再次访问转向页面的action这个。在这个action中根据利用js传过来的值再查询数据库,将查询的结果存储到form或者存储到request里,最后再在页面中显示出来。在思路和servlet实现没有什么区别,关键区别在于在最后将出发onchange之前页面的数据重新再付给form,这样页面的数据就不会因为刷新而丢失了。
下面是相关代码:
1.action
     
  1. //卡类型列表信息   
  2.        List typeInfoList = new ArrayList();   
  3.        CardType cardtype = new CardType();   
  4.        cardtype.setCardTyoeSts("A");   
  5.        typeInfoList = this.typeDelegate().findCardTypeByPO(cardtype,   
  6.                getLocalNetId(req));   
  7.        log.debug("typeInfoList.size()=============" + typeInfoList.size());   
  8.        form.setTypeInfoList(typeInfoList);   
  9.        req.setAttribute("typeInfoList", typeInfoList);   
  10.        String cardType = req.getParameter("cardType");   
  11.        List praInfoList;   
  12.        if ((cardType!=null)&&!(cardType.equals(""))) {   
  13.            //动态显示卡面值信息   
  14.            CardParvalue cardPravalue=new CardParvalue();   
  15.            CardType ct=new CardType();   
  16.            ct.setCardTypeId(new Long(cardType));   
  17.            praInfoList = new ArrayList();   
  18.            cardPravalue.setCardParvalueSts("A");   
  19.            cardPravalue.setCardType(ct);   
  20.            praInfoList = this.praDelegate().findCardParvalueByVO(cardPravalue,   
  21.                    getLocalNetId(req));   
  22.            log.debug("---------------praInfoList.size==========" +   
  23.                      praInfoList.size());   
  24.          form.setCardTypeName(cardType);              
  25.            form.setParaValueInfoList(praInfoList);   
  26.        }   
  27.        else{   
  28.            praInfoList=null;   
  29.        }   
  30.        req.setAttribute("praInfoList", praInfoList);  

2. 页面代码

  1.                   <td align="right">卡类型:td>  
  2. <td align="left">  
  3.   <logic:equal name="cardBatchForm" scope="request" value="P">  
  4.     <html:select property="cardTypeName" style="width:120px " onchange="select()">  
  5.       <html:option value="">请选择...html:option>  
  6.       <html:options collection="typeInfoList" property="cardTypeId" labelProperty="cardTypeName"/>  
  7.     html:select>  
  8.   logic:equal>  
  9.   <logic:notEqual name="cardBatchForm" property="centerFlag" value="P">  
  10.     <html:select property="cardTypeName" style="width:120px "onchange="select()">  
  11.                         <html:option value="">请选择...html:option>  
  12.       <html:options collection="typeInfoList" property="cardTypeId" labelProperty="cardTypeName"/>>  
  13.     html:select>  
  14.   logic:notEqual>  
  15. td>  
  16. <logic:empty name="praInfoList">  
  17. <td align="right">卡面值:td>  
  18. <td align="left">  
  19.   
  20.     <html:select property="cardParvalue" style="width:120px ">  
  21.       <html:option value="">请选择...html:option>  
  22.      html:select>  
  23.   
  24. td>  
  25. logic:empty>  
  26. <logic:notEmpty name="praInfoList">  
  27. <td align="right">卡面值:td>  
  28. <td align="left">  
  29.   <logic:equal name="cardBatchForm" scope="request" value="P">  
  30.     <html:select property="cardParvalue" style="width:120px ">  
  31.       <html:option value="">请选择...html:option>  
  32.       <html:options collection="praInfoList" property="cardParvalueId" labelProperty="cardParvalue"/>  
  33.     html:select>  
  34.   logic:equal>  
  35.   <logic:notEqual name="cardBatchForm" property="centerFlag" value="P">  
  36.     <html:select property="cardParvalue" style="width:120px ">  
  37.       <html:option value="">请选择...html:option>  
  38.       <html:options collection="praInfoList" property="cardParvalueId" labelProperty="cardParvalue"/>  
  39.     html:select>  
  40.   logic:notEqual>  
  41. td>  
  42. logic:notEmpty>  

3. 调用的js

  1. function select(){   
  2.   thisForm=document.forms[0];   
  3.   var cardType=thisForm.cardTypeName.value;   
  4.   
  5.   window.location="xxAction.do?method=xx&cardType="+cardType;   
  6. }  

4. 后记
    其实想想用servlet同样可以实现这样的效果,用struts与之区别关键就是在于他用了form来对数据进行存储和显示,同样可以建立一个javabean来存储这些数据来避免刷新数据的丢失的。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值