在这里我是用ajax来处理级联的,这里关注的问题是从数据库中取出数据在jsp页面中级联显示,下面是我写的一些代码:
在service中:
//查找所有违章信息 public List findAllInfor(){ return informationDao.findAll(); } //得到第一条违章记录 public InformationTable findOneInfor(){ List list=findAllInfor(); return (InformationTable) list.get(0); }
在jsp页面中:
<input type="button" value="添加" onclick="abreak()">
<select name="acod" id="acod" onchange="aInforChange()"> <s:iterator value="#request.information" var="ainfor"> <option value="${ainfor.incode }" id="${ainfor.inid }">${ainfor.incode }</option> </s:iterator> </select>
var acod=document.getElementById("acod"); var aill=document.getElementById("aill"); var abas=document.getElementById("abas"); var arec=document.getElementById("arec"); var amon=document.getElementById("amon"); var apun=document.getElementById("apun");
//添加船舶 function abreak(){ showBreak.style.display="none"; updateBreak.style.display="none" addBreak.style.display="block"; //创建核心对象 var request=null; try{ request=new XMLHttpRequest(); }catch(e){ request=new ActiveXObject("microsoft.XMLHttp"); } //打开并发送 request.open("get","break!firstInfor.do"); //处理乱码 request.setRequestHeader("Content-Type","text/html;charset=gb2312"); request.send(null); //接收 request.onreadystatechange=function(){ if(request.readyState==4&&request.status==200){ txt=request.responseText; var arr=new Array(); arr=txt.split("|||"); aill.value=arr[0]; abas.value=arr[1]; arec.value=arr[2]; amon.value=arr[3]; apun.value=arr[4]; } } }
//级联显示违章信息的内容(添加) function aInforChange(){ var inid; for(var i=0;i<acod.options.length;i++){ if(acod.options[i].selected){ inid=acod.options[i].id; } } //创建核心对象 var request=null; try{ request=new XMLHttpRequest(); }catch(e){ request=new ActiveXObject("microsoft.XMLHttp"); } //打开并发送 request.open("get","break!jilianInfor.do?inid="+inid); //处理乱码 request.setRequestHeader("Content-Type","text/html;charset=gb2312"); request.send(null); //接收 request.onreadystatechange=function(){ if(request.readyState==4&&request.status==200){ txt=request.responseText; arr=new Array; arr=txt.split("|||"); aill.value=arr[0]; abas.value=arr[1]; arec.value=arr[2]; amon.value=arr[3]; apun.value=arr[4]; } } }
在Action中:
//级联显示第一条违章信息 public void firstInfor(){ HttpServletRequest request=ServletActionContext.getRequest(); HttpServletResponse response=ServletActionContext.getResponse(); InformationTable information=breakService.findOneInfor(); String inill=information.getInillegal(); String inbas=information.getInbasis(); String inrec=information.getIngrade().toString(); String inmon=information.getInmoney().toString(); String inpun=information.getInpunish(); StringBuffer sBuffer=new StringBuffer(); sBuffer.append(inill); sBuffer.append("|||"); sBuffer.append(inbas); sBuffer.append("|||"); sBuffer.append(inrec); sBuffer.append("|||"); sBuffer.append(inmon); sBuffer.append("|||"); sBuffer.append(inpun); String string=sBuffer.toString(); try { response.getWriter().print(string); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //级联显示违章信息 public void jilianInfor(){ HttpServletRequest request=ServletActionContext.getRequest(); HttpServletResponse response=ServletActionContext.getResponse(); String strinid=request.getParameter("inid"); if(strinid!=null){ int inid=Integer.parseInt(strinid); InformationTable information=breakService.findInfor(inid); String inill=information.getInillegal(); String inbas=information.getInbasis(); String inrec=information.getIngrade().toString(); String inmon=information.getInmoney().toString(); String inpun=information.getInpunish(); StringBuffer sBuffer=new StringBuffer(); sBuffer.append(inill); sBuffer.append("|||"); sBuffer.append(inbas); sBuffer.append("|||"); sBuffer.append(inrec); sBuffer.append("|||"); sBuffer.append(inmon); sBuffer.append("|||"); sBuffer.append(inpun); String string=sBuffer.toString(); try { response.getWriter().print(string); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }else { try { response.getWriter().print("err"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }