java ---------------------------------------------------------- public void getAreas(HttpServletRequest req,HttpServletResponse res) throws Exception { //省代碼 String provinceCode=req.getParameter("provinceCode"); StringBuffer strBuffer = new StringBuffer(); strBuffer.append(" and PROVINCEID = '" + provinceCode + "' "); String conditions = " 1=1 "; conditions += strBuffer.toString();java
BLSalevelFacade blSalevelFacade = new BLSalevelFacade(); Collection collection = blSalevelFacade.findAreasByProvince(conditions); this.getJSONAreas(collection, res); }
public void getJSONAreas(Collection collection ,HttpServletResponse response) throws IOException{ response.setContentType("test/xml;charset=utf-8"); response.setHeader("Cache-Control", "no-cache"); Iterator it = collection.iterator(); Collection al = new ArrayList(); PrintWriter out = response.getWriter(); org.json.JSONObject jo = null ; while(it.hasNext()){ SaUserDto saUserDto = (SaUserDto)it.next(); jo = new JSONObject(saUserDto); al.add(jo); } JSONArray jsonArray = new JSONArray(al); out.println(jsonArray); out.flush(); out.close(); }json
js-------------------------------------------------------------------------- function showAreas(){ //取得區域 var listform = fm.bankOfCity; //取得省代碼 var provinceCode = fm.bankOfProvince.value; for(var i=listform.options.length-1;i>=1;i--) { listform.remove(i); } $.getJSON("/sales/processSaUser.do?actionType=getAreas&provinceCode="+provinceCode+"&format=json", function(list){ for(var i=0;i<list.length;i++){ var str1 = $(list).get(i).code; var str2 = $(list).get(i).name; var option = document.createElement("option"); option.text =str2; option.value=str1; listform.add(option); } }); }app