In springmvc, the controller's result set can be passed to the js front-end through json format, or it can be passed to the front-end through map. The specific implementation is as follows
1. Pass through json format
Controller layer is implemented as follows
@RequestMapping("queryCityInfo") @ResponseBody public String queryCityInfo()throws Exception{ String provinceId = getString("id"); @SuppressWarnings("rawtypes") List cityList = personalService.queryCity(provinceId); if(null != cityList && cityList.size() >0 ){ String json = JSONUtils.toJSONString(cityList); super.outStr(json); } return null; } protected void outStr(String str)</span> { try { response.setCharacterEncoding("UTF-8"); response.getWriter().write(str); } catch (Exception e) { } } public static <T> String toJSONString(List<T> list) { JSONArray jsonArray = JSONArray.fromObject(list); return jsonArray.toString(); } The js side accepts the following
function selectBankCity(id){ $.ajax({ url:baseAddress+"queryCityInfo.do?provinceId="+id, type:'get', dataType:'json', success:function(data){ $('#custBankArea').empty(); $('#custBankArea').append("<option >--Please select City Information--</option>"); for(var i=0;i<data.length;i++){ $('#custBankArea').append("<option value='"+data[i].id+"'>"+data[i].cityName+"</option>"); } } } }); } 2. Pass through Map
Controller layer is implemented as follows
@RequestMapping("queryProvince") @ResponseBody public Map<String, Object> queryProvince(HttpServletRequest request,HttpServletResponse response){ Map<String, Object> map = new HashMap<String, Object>(); try { @SuppressWarnings("rawtypes") List provinceList = personalService.queryProvince(); if(null != provinceList && provinceList.size() >0 ){ map.put("province", provinceList); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return map; } The js side accepts the following
$.ajax({ url:baseAddress+"queryProvince.do", type:"get", success:function(resData){ var data = resData.province; for(var i=0;i<data.length;i++){ //js implementation//var objs = document.getElementById("cusBankCity") //objs.options.add(new Option(data[i].provinceName) ,data[i].id); //jq implementation $("#cusBankCity").append("<option value='"+data[i].id+"'>"+data[i].provinceName+"</option>"); } } });The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.