@RequestMapping(value = "importTest", method = RequestMethod.POST,produces = "text/html;charset=UTF-8") @ResponseBody public String importTest(@RequestParam(required = false)MultipartFile file,BsAllguaranteechange bsAllguaranteechange, Model model, RedirectAttributes redirectAttributes) { JSONObject json = new JSONObject(); Map<String, Object> returnMap = Maps.newHashMap(); int result = 0; if (file.getSize()==0) { json.put("isSuccess", true); json.put("msg", "无添加"); return json.toString(); } try { ImportExcel ei = new ImportExcel(file, 1, 0); List<BsAllguaranteechange> bsAllguaranteechangelist = ei.getDataList(BsAllguaranteechange.class); for (int i = 0; i < bsAllguaranteechangelist.size(); i++) { //....省略 } } catch (Exception e) { addMessage(redirectAttributes, OperationMessage.ERROR.toString()); e.printStackTrace(); } if(result!=0){ json.put("isSuccess", true); json.put("x", "su"); json.put("msg", "添加成功"); return json.toString(); } CommonFunctionUtil.operateDB(returnMap, result); return json.toString(); }
前台代码:
<form id="importForm" action="${ctx}/business/bsAllguaranteechange/importTest" modelAttribute="bsAllguaranteechange" method="post" enctype="multipart/form-data" class="form-horizontal" style="padding-left:20px;text-align:center;" onsubmit="loading('正在导入,请稍等...');"><br/> <input id="uploadFile" name="file" type="file" style="width:330px"/><br/><br/> <input id="btnImportSubmit" class="btn btn-primary" type="submit" value=" 导 入 "/> </form> <script type="text/javascript"> $("#btnImportSubmit").click(function () {$('#importForm').form('submit', { url:"${ctx}/business/bsAllguaranteechange/importTest", success:function(result){ alert(1); } }); }); </script>
我想返回一个错误提示信息 就是弹出框那种,但是现在返回的一直是一个有提示信息的新界面,script貌似不起作用,有没有大神帮忙解答一下,在线等~
应该是你后台java成功后直接返回页面了。导致js没起作用
谢谢帮助~
var formData = new FormData($( "#importForm" )[0]);
$.ajax({
url: '${ctx}/business/bsAllguaranteechange/importTest' ,
type: 'POST',
data: formData,
async: false,
cache: false,
contentType: false,
processData: false,
success: function (returndata) {
var msg = eval('(' + returndata + ')');
var mess = '\''+msg.msg+'\'';
if(msg.x == "zero"){
$.msg_show.Init({
'msg':mess,
'type':'error'
});
}
if(msg.x == "su"){
$.msg_show.Init({
'msg':mess,
'type':'success'
});
}
},
error: function (returndata) {
$.msg_show.Init({
'msg':"导入失败!",
'type':'success'
});
}
});
问题已解决,前台换用formData 及ajax提交后返回正确弹出信息,不新出界面。不知道原因~ 谢谢帮助~