前段时间写的关于JSP+servlet+javaBean实现联动的例子,提到存在一个问题就是当再次访问servlet后转回页面后原本页面的数据被刷新掉了。当时我设想如果使用struts实现的话由于他的form的特制,可以避免由于刷新而冲掉数据。最近由于需要对这个进行了实现。
基本思路:
首先从页面中利用js获得下拉列表的选中值,然后触发onchange事件,这个事件再次访问转向页面的action这个。在这个action中根据利用js传过来的值再查询数据库,将查询的结果存储到form或者存储到request里,最后再在页面中显示出来。在思路和servlet实现没有什么区别,关键区别在于在最后将出发onchange之前页面的数据重新再付给form,这样页面的数据就不会因为刷新而丢失了。
下面是相关代码:
1.action
基本思路:
首先从页面中利用js获得下拉列表的选中值,然后触发onchange事件,这个事件再次访问转向页面的action这个。在这个action中根据利用js传过来的值再查询数据库,将查询的结果存储到form或者存储到request里,最后再在页面中显示出来。在思路和servlet实现没有什么区别,关键区别在于在最后将出发onchange之前页面的数据重新再付给form,这样页面的数据就不会因为刷新而丢失了。
下面是相关代码:
1.action
- //卡类型列表信息
- List typeInfoList = new ArrayList();
- CardType cardtype = new CardType();
- cardtype.setCardTyoeSts("A");
- typeInfoList = this.typeDelegate().findCardTypeByPO(cardtype,
- getLocalNetId(req));
- log.debug("typeInfoList.size()=============" + typeInfoList.size());
- form.setTypeInfoList(typeInfoList);
- req.setAttribute("typeInfoList", typeInfoList);
- String cardType = req.getParameter("cardType");
- List praInfoList;
- if ((cardType!=null)&&!(cardType.equals(""))) {
- //动态显示卡面值信息
- CardParvalue cardPravalue=new CardParvalue();
- CardType ct=new CardType();
- ct.setCardTypeId(new Long(cardType));
- praInfoList = new ArrayList();
- cardPravalue.setCardParvalueSts("A");
- cardPravalue.setCardType(ct);
- praInfoList = this.praDelegate().findCardParvalueByVO(cardPravalue,
- getLocalNetId(req));
- log.debug("---------------praInfoList.size==========" +
- praInfoList.size());
- form.setCardTypeName(cardType);
- form.setParaValueInfoList(praInfoList);
- }
- else{
- praInfoList=null;
- }
- req.setAttribute("praInfoList", praInfoList);
2. 页面代码
- <td align="right">卡类型:td>
- <td align="left">
- <logic:equal name="cardBatchForm" scope="request" value="P">
- <html:select property="cardTypeName" style="width:120px " onchange="select()">
- <html:option value="">请选择...html:option>
- <html:options collection="typeInfoList" property="cardTypeId" labelProperty="cardTypeName"/>
- html:select>
- logic:equal>
- <logic:notEqual name="cardBatchForm" property="centerFlag" value="P">
- <html:select property="cardTypeName" style="width:120px "onchange="select()">
- <html:option value="">请选择...html:option>
- <html:options collection="typeInfoList" property="cardTypeId" labelProperty="cardTypeName"/>>
- html:select>
- logic:notEqual>
- td>
- <logic:empty name="praInfoList">
- <td align="right">卡面值:td>
- <td align="left">
- <html:select property="cardParvalue" style="width:120px ">
- <html:option value="">请选择...html:option>
- html:select>
- td>
- logic:empty>
- <logic:notEmpty name="praInfoList">
- <td align="right">卡面值:td>
- <td align="left">
- <logic:equal name="cardBatchForm" scope="request" value="P">
- <html:select property="cardParvalue" style="width:120px ">
- <html:option value="">请选择...html:option>
- <html:options collection="praInfoList" property="cardParvalueId" labelProperty="cardParvalue"/>
- html:select>
- logic:equal>
- <logic:notEqual name="cardBatchForm" property="centerFlag" value="P">
- <html:select property="cardParvalue" style="width:120px ">
- <html:option value="">请选择...html:option>
- <html:options collection="praInfoList" property="cardParvalueId" labelProperty="cardParvalue"/>
- html:select>
- logic:notEqual>
- td>
- logic:notEmpty>
3. 调用的js
- function select(){
- thisForm=document.forms[0];
- var cardType=thisForm.cardTypeName.value;
- window.location="xxAction.do?method=xx&cardType="+cardType;
- }
4. 后记
其实想想用servlet同样可以实现这样的效果,用struts与之区别关键就是在于他用了form来对数据进行存储和显示,同样可以建立一个javabean来存储这些数据来避免刷新数据的丢失的。