Ajax 多級聯動(Struts1.x與Strurts2.x)以市為例子上代碼:(如有問題可以加QQ:2250680083) 1、匯入包json-lib-2.2.3-jdk15.jar,引入jQuery如果連網建議使用Google的,地址為:<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> 2、頁面代碼如下:var url="/" +lemis.WEB_APP_NAME + "/monitor/comEditShi.do"; //初始化市區 function inni_city(){ jQuery.ajax({ url : url, data:{method: 'ajaxGetCity'}, type : "post", async: false, cache : false, dataType : "json", success:onchangecallback }); function onchangecallback(data){ var str="<option value=''>請選擇</option>" ; for(var i=0;i<data.length;i++){ if(s8==(data[i].key)){ str+= "<option selected=true value='"+data[i].key+"'>"+data[i].value+ "</option>";//應對IE6的方法($("").val(值)會有問題) }else{ str+= "<option value='"+data[i].key+"'>"+data[i].value+ "</option>"; } } $( "#所放位子ID").html(str); } } 3、配置action(自己配置) 4、Java代碼public class ComEditShi extends ActionLeafSupport {
/***
*擷取市區
**/
public ActionForward ajaxGetCity(ActionMapping mapping, ActionForm actionForm,
HttpServletRequest request, HttpServletResponse response) throws AppException, IOException{
List<Param> zihanTypes =new ArrayList<Param>();
AjaxData msg1 = new AjaxData();//自己的方法
AjaxData.java |
public class AjaxData { /** * 省廳監測,擷取市 * @author 紫寒 * @return */ public LinkedHashMap getShi_st() { AjaxDataDAO rd = new AjaxDataDAO(); try { return rd.getShi_st();//自己方法
public LinkedHashMap getShi_st() throws SQLException { LinkedHashMap map = getData("ajax_shi",null);
//市縣區 紫寒 private LinkedHashMap getData(String string,String string2){ LinkedHashMap map = new LinkedHashMap(); Sc01 aSc01 = new Sc01(); Sc01 aSc01_ = new Sc01(); aSc01.setFileKey(string); if(string2!=null){ aSc01.setAab003(string2); } Connection con = null; try { con = DBUtil. getConnection(); List<?> list = (List<?>) find(con, aSc01, null, 0); for (int i = 0; i < list.size(); i++) { ClassHelper.copyProperties(list.get(i), aSc01_); map.put(aSc01_.getAab003(), aSc01_.getAab300());//主要的一步 } } catch (NoConnectionException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (AppException e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } |
return map; } |
} catch (SQLException e) { e.printStackTrace(); } return null; } } |
LinkedHashMap mapaab061 = msg1.getShi_st();
Set<Map.Entry<String, String>> entryseSet=mapaab061.entrySet();
for (Map.Entry<String, String> entry:entryseSet) {
Param param = new Param();
Param.java |
public class Param { private Object entity; //預留 private String value; private String key; public Param() { } public Param(String value, String key) { this.value = value; this.key = key; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getkey() { return key; } public void setkey(String key) { this.key = key; } public Object getEntity() { return entity; } public void setEntity(Object entity) { this.entity = entity; } } |
param.setkey(entry.getKey());
param.setValue(entry.getValue());
zihanTypes.add(param);
}
JSONArray jsonObj = JSONArray.fromObject(zihanTypes);
sendMsg(jsonObj.toString(),response); //Structs1.x // sendMsg(jsonObj.toString()); //Structs2.x
Structs1.x |
Structs2.x |
private void sendMsg(String string, HttpServletResponse response) { // TODO Auto-generated method stub response.setCharacterEncoding("UTF-8"); try { response.getWriter().write(string); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
private void sendMsg(String string) { HttpServletResponse response = ServletActionContext.getResponse(); response.setCharacterEncoding("UTF-8"); response.getWriter().write(string); } |
return null;
}
}5、相應的級聯方法用jQuery的onchange事件就OK了!6、祝技術分享愉快!