要發送的數據為:String topicId,String topicName,String summarize,List<ModuleParam> parentList
前端頁面ajax請求數據,代碼:
1 exportData('${ctx}/report/compositeReport/export.do',{topicId:'${topicId}', topicName:'${topicName}', summarize:$("#summarizeContent").html(), 'parentList':$parentList}); 2 3 function exportData(url,params){ 4 common.showLoading('正在生成文件, 請稍后...'); 5 $.ajax({ 6 'url':url, 7 'type':'post', 8 'data':JSON.stringify(params), 9 'dataType':'json', 10 'contentType':'application/json;charset=utf-8', 11 success:function(result){ 12 common.hideLoading(); 13 if (result.success) { 14 var filename= result.data; 15 common.dialogEdit('導出報告', "${ctx}/report/compositeReport/download.do?fileName="+filename); 16 } else { 17 common.showMsg('導出失敗!'); 18 } 19 }, 20 error:function(res){ 21 common.showMsg(res.responseText); 22 } 23 }); 24 }
controller代碼:
1 @RequestMapping("/compositeReport/export") 2 @ResponseBody 3 public JsonResult export(@RequestBody Map<String,Object> json, HttpServletRequest request, HttpServletResponse response){ 4 JsonResult result = JsonResult.createWithFail(); 5 List<ModuleParam> moduleList = JSONObject.parseArray(json.get("parentList").toString(), ModuleParam.class); 6 String topicName = json.get("topicName").toString(); 7 String topicId = json.get("topicId").toString(); 8 String summarize = json.get("summarize").toString(); 9 。。。。。。。
json參數的變量名可以隨便寫,這個list其實用json.get("parentList")的結果強轉也可以得到,但是會報類型安全警告看着不爽,加個suppress注解也麻煩,就這樣轉換一下吧。
參考:4種方法讓SpringMVC接收多個對象 http://blog.csdn.net/lutinghuan/article/details/46820023
