前端發送的數據:
前端JS請求:
1 //demo為JSON格式數據 2 let para ={ 3 dataJ: JSON.stringfiy(demo); 4 } 5 //這一段是Vue封裝的方法,本質就是一條url 6 this.$http.post('${webRoot}/demo?list', para,{emulateJSON: true});
后端Controller接收數據:
1 @RequestMapping(params = "list") 2 @ResponseBody 3 public void treeList(String treedata , HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid){ 4 // request.getParameterValues("treedata"); 5 JSONArray jsonArray = JSONArray.fromObject(treedata); 6 for (Object o : jsonArray) { 7 JSONObject jo = (JSONObject)o; 8 System.out.println(jo.getString("parent")); 9 } 10 }
=================分割線===========================
反之
后端Controller封裝數據:
1 @RequestMapping(params = "query") 2 public void query(HttpServletRequest request) { 3 //父級 4 Map<String,Object> parentMap=new HashMap<String, Object>(); 5 parentMap.put("id", "001"); 6 parentMap.put("name", "王某"); 7 //子級 8 Map<String,Object> childMap=new HashMap<String, Object>(); 9 childMap.put("id", "child001"); 10 childMap.put("name", "王孩子"); 11 //將子級Map封裝成List 12 List<Map<String,Object>> ChildMapList=new ArrayList<Map<String,Object>>(); 13 ChildMapList.add(childMap); 14 //將子級MapList寫進父級Map中 15 parentMap.put("children", ChildMapList); 16 //將父級Map封裝成List 17 List<Map<String,Object>> parentMapList=new ArrayList<Map<String,Object>>(); 18 parentMapList.add(parentMap); 19 //將封裝好的數據轉換成Json 20 JSONArray arr = JSONArray.fromObject(parentMapList); 21 request.setAttribute("mapList", arr); 22 23 request.setAttribute("mapList", arr); 24 25 }
前端頁面el表達式接收 ${mapList} 后數據格式:
----------展開后:
我是應用在樹形菜單的,這一部分代碼就不展示了。效果如下: