该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2007-06-13
首先页面显示时,加载省的选项,然后选择省后,将省的值传到Action中,调用方法获取市的集合,再将市的集合赋值给FormBean,再将FormBean传到原JSP页面,从而达到动态效果,但是现在出了点问题,代码如下: 我有一XML文件: <?xml version="1.0" encoding="GBK"?> <china> <province name="直辖市"> <city>北京</city> <city>上海</city> <city>天津</city> <city>重庆</city> </province> <province name="广东"> <city>广州</city> <city>珠海</city> <city>深圳</city> <city>东莞</city> </province> <province name="广西"> <city>桂林</city> <city>柳州</city> <city>北海</city> <city>南宁</city> </province> <province name="海南"> <city>海口</city> <city>三亚</city> </province> <province name="湖北"> <city>武汉</city> <city>鄂州</city> <city>荆州</city> <city>十堰</city> </province> <province name="湖南"> <city>长沙</city> <city>岳阳</city> <city>常德</city> <city>张家界</city> </province> <province name="浙江"> <city>杭州</city> <city>绍兴</city> <city>宁波</city> <city>台州</city> </province> <province name="辽宁"> <city>沈阳</city> <city>大连</city> <city>抚顺</city> <city>铁岭</city> </province> </china> *************************************** 然后通过JDOM解析,分别获取省市, package com.selectdemo.tool; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class ReadXml { Element root = null; public ReadXml() throws FileNotFoundException, JDOMException, IOException { super(); SAXBuilder sb = new SAXBuilder();//解析器对象 Document doc = sb.build(this.getClass().getResourceAsStream("/city.xml"));//绑定文件 root = doc.getRootElement();//获取根元素 } public List getProvince(){ //获取省份 ArrayList provinceList = new ArrayList(); List tempList = root.getChildren();//获取所有省份节点 for(int i=0; i<tempList.size();i++){ Element province=(Element)tempList.get(i);//子节点转型 provinceList.add(province.getAttributeValue("name"));//获取省份节点属性内容 } return provinceList; } public List getCity(String province){ ArrayList cityList = new ArrayList(); List provincetemplist = root.getChildren();//省份集合 for(int i=0; i<provincetemplist.size();i++){ Element provinceElement = (Element)provincetemplist.get(i); if((provinceElement.getAttributeValue("name")).equals(province)){//如果属性为传进来的名称 List cityTempList = provinceElement.getChildren();//获取子节点集合 for(int j=0; j<cityTempList.size(); j++){//循环 Element cityElement = (Element)cityTempList.get(j);//当前城市节点 cityList.add(cityElement.getTextTrim());//增加城市到集合 } } } return cityList; } } ****************************** 然后JSP页面代码: <html:html> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK"> <title>Insert title here</title> <SCRIPT LANGUAGE="JavaScript"> function fungetCityes() { document.forms[0].action="/select.do"; document.forms[0].sumbit(); } </SCRIPT> </head> <body> <html:form action="/select.do" method="post"> <html:select property="province" onchange="fungetCityes()"> <html:option value="">请选择省份</html:option> <html:options property="provinces" /> </html:select> <html:select property="city"> <html:option value="">请选择城市</html:option> <html:options property="cities" /> </html:select> </html:form> </body> </html:html> **************************** 接着FormBean: package com.selectdemo.struts.form; import java.io.FileNotFoundException; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import org.apache.struts.action.ActionForm; import org.jdom.JDOMException; import com.selectdemo.tool.ReadXml; public class SelectForm extends ActionForm { private String province; private String city; private Collection provinces; private Collection cities = new ArrayList(); public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public Collection getCities() { try { ReadXml readXml = new ReadXml(); cities = readXml.getCity(province); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return cities; } public void setCities(Collection cities) { this.cities = cities; } public Collection getProvinces() { try { ReadXml readXml = new ReadXml(); provinces = readXml.getProvince(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return provinces; } public void setProvinces(Collection provinces) { this.provinces = provinces; } } ************************************************* Action: package com.selectdemo.struts.action; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.jdom.JDOMException; import com.selectdemo.struts.form.SelectForm; import com.selectdemo.tool.ReadXml; public class SelectAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { SelectForm selectForm = (SelectForm) form; String province = selectForm.getProvince(); System.out.println(province); try { ReadXml readXml = new ReadXml(); List list = readXml.getCity(province);//获取城市集合 selectForm.setCities(list);//将城市集合设置给Form request.setAttribute("selectForm", selectForm);//返回给原JSP页面 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return mapping.findForward("success"); } } ************************* struts-config.xml <form-bean name="selectForm" type="com.selectdemo.struts.form.SelectForm" /> <action input="/select.jsp" name="selectForm" path="/select" scope="request" type="com.selectdemo.struts.action.SelectAction"> <forward name="success" path="/select.jsp" /> </action> ********************************************** <SCRIPT LANGUAGE="JavaScript"> function fungetCityes() { document.forms[0].action="/select.do"; document.forms[0].sumbit(); } </SCRIPT> 感觉这里有问题,但不知道怎么写. 现在的问题是:省份能够取出,并在JSP页面正常显示,但是当我选择省份后,却左下角显示"页面错误",应该是JavaScript的问题,可是我不知道出错在哪里,请看一下,谢谢. 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2007-06-14
兄弟,“页面错误”里面是怎么说的?IE版本是?
可不可以把页面的运行结果HTML代码贴出? |
|
返回顶楼 | |
发表时间:2007-06-14
|
|
返回顶楼 | |
发表时间:2007-06-14
在学校IE6.0,状态栏左边会出现一个黄色的感叹号,然后什么页面错误,在家IE7.0,什么都没有了,就是出不了结果,城市出不来.
Struts能用JavaScript用Array数组实现的省市联动菜单吗? |
|
返回顶楼 | |
发表时间:2007-06-14
onchange()会触发JavaScript方法, document.forms[0].action="/select.do"; 当前表单的提交路径, document.forms[0].sumbit(); 提交 此时应该会传到Action,可是我这边怎么也到不了Action |
|
返回顶楼 | |
发表时间:2007-06-14
当前表单的提交路径有问题吧
|
|
返回顶楼 | |
发表时间:2007-06-14
那是不要这一句话吗?如果加这一句话,是不是应该
document.forms[0].action="/别的路径"; |
|
返回顶楼 | |
发表时间:2007-06-14
去掉了这句,还是出错啊.
|
|
返回顶楼 | |
发表时间:2007-06-14
用document.forms[0].action=""; 没问题,我得意思是指定的路径“/select.do”是不是不对啊,我是写的完整路径"<%=request.getContextPath()%>/select.do"
|
|
返回顶楼 | |
发表时间:2007-06-21
大侠!你是不是把submit写错了哦!嘿嘿!但是你那个估计得用框架实现哦。因为你提交到action中的话它会提交的。提交之后本页面是不会在保存你前面那个下拉列表中的数据的。
|
|
返回顶楼 | |