標籤:ajax context filename try 傳值 name form表單 turn 衝突
springMVC在使用ajax進行後台傳值的時候發現找不到對應的requestMapping(""),無法進入後台,在多次實驗後確定是
MultipartFile對象與ajax衝突;並且不報任何異常,直接調用ajax的失敗屬性方法處理;
代碼如下:
·1 @RequestMapping("/addimage") 2 public String addimage(@RequestParam("image")MultipartFile file,HttpServletRequest request){ 3 4 PersonMsgService.setEntityClass(Personalmsg.class); 5 Personalmsg bean=(Personalmsg) PersonMsgService.get(getInt(request, "personMsgID")); 6 7 String fileName =getInt(request, "personMsgID")+".JPG"; 8 System.out.println(fileName); 9 10 String path = request.getSession().getServletContext().getRealPath("upload");11 System.out.println(path);12 File targetFile = new File(path,fileName);13 if(!targetFile.exists()){14 targetFile.mkdirs();15 }16 try {17 file.transferTo(targetFile);18 bean.setImage(path+"\\"+fileName);19 System.out.println(bean);20 PersonMsgService.saveOrUpdate(bean);21 } catch (IllegalStateException e) {22 e.printStackTrace();23 } catch (IOException e) {24 e.printStackTrace();25 }26 return "redirect:/test/PersonMsg/tolist.spring";27 }
@RequestParam("image")MultipartFile file這個參數會導致按鍵按下ajax無法匹配對應的@RequestMapping("/addimage");
使用
MultipartRequest mrq=(MultipartRequest) request;對request轉換也無法成功
當前我的解決方案是放棄使用ajax直接使用form表單提交;
springMVC含檔案上傳調用ajax無法串連後台