public String loadCity() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); String provinceId = request.getParameter("provinceId"); Province province = provinceService.getById(Integer.valueOf(provinceId)); Set citySet = province.getCities(); //把citySet集合转成Json传给页面 //只取得City的id和name属性 SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); Set set = filter.getIncludes(); set.add("id"); set.add("name"); writeHtml(JSONArray.toJSONString(citySet,filter)); return null; } public String loadArea() throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); String cityId = request.getParameter("cityId"); City city = cityService.getById(Integer.valueOf(cityId)); Set areaSet = city.getAreas(); //把areaSet集合传给页面 //只选取Area的name和id和code属性 SimplePropertyPreFilter filter = new SimplePropertyPreFilter(); Set set = filter.getIncludes(); set.add("id"); set.add("name"); writeHtml(JSONArray.toJSONString(areaSet,filter)); return null; } //返回strData结果给页面 protected void writeHtml(String strData) throws Exception { HttpServletResponse response = ServletActionContext.getResponse(); response.setContentType("application/json;charset=UTF-8"); response.setHeader("Cache-Control", "no-cache"); PrintWriter out = response.getWriter(); out.write(strData); out.flush(); out.close(); }
========
二 、 遍历Action的属性provinceList 获取:
=========================
jsp:在隐藏域保存遍历list
js:
$(function(){ $("#provinceList").change(function (){ var value = $(this).val(); $("#cityList").html(""); $("input[name^='province_"+value+"']").each(function(){ var val = $(this).val(); $("#cityList").append(""); }); $("#areaList").html(""); }); $("#cityList").change(function (){ var value = $(this).val(); $("#areaList").html(""); $("input[name^='city_"+value+"']").each(function(){ var val = $(this).val(); $("#areaList").append(""); }); });});
========
三 、 Action中将provinceList 转成JSON字符串传给页面保存:
=========================
jsp:
<%-- 直接在js里保存后台传来JSON的字符串 页面无需保存 --%>
js:将后台传来的字符串保存到js对象中
$(function(){ var provinceJSONString = $("#provinceJSONString").val();//将java的String对象转成js字符对象 var provinceJSON = eval('('+provinceJSONString+')');//转换为json对象 /** * 首次加载页面,填充province列表 * 加载一级列表 */ loadOptions(provinceJSON,$("#provinceList")); function loadOptions(data,item){ $.each(data, function(index,value){ item.append($("").val(index+1).text(value.name)); }); } /** * 加载二级列表 */ $("#provinceList").change(function (){ var value = $(this).val(); $("#cityList").html(""); loadOptions(provinceJSON[value-1].cities,$("#cityList")); $("#areaList").html(""); });
action:将provinceList转成JSON格式的字符串
/** * provinceList转成JSON字符串 * @param _provinceList * @return * @throws Exception */public static String toProvinceJSONString(List _provinceList) throws Exception{ if (_provinceList == null) return null; Iterator itrProvince = _provinceList.iterator(); StringBuffer strBuffer = new StringBuffer("["); while(itrProvince.hasNext()) { Province _province = itrProvince.next(); strBuffer.append("{\"id\":\""+_province.getId()+"\",\"name\":\""+_province.getName()+"\",\"cities\":["); Set Cities = _province.getCities(); Iterator itrCity = Cities.iterator(); while(itrCity.hasNext()) { City _city = itrCity.next(); strBuffer.append("{\"id\":"+_city.getId()+",\"name\":\""+_city.getName()+"\",\"areas\":["); Set Areas = _city.getAreas(); Iterator itrArea = Areas.iterator(); while(itrArea.hasNext()) { Area _area = itrArea.next(); strBuffer.append("{\"id\":\""+_area.getId()+"\",\"name\":\""+_area.getName()+"\"}"); if (itrArea.hasNext()) { strBuffer.append(","); } } strBuffer.append("]}"); if (itrCity.hasNext()) { strBuffer.append(","); } } strBuffer.append("]}"); if (itrProvince.hasNext()) { strBuffer.append(","); } } strBuffer.append("]"); return strBuffer.toString();}