SpringMVC幾種數據接收方式java
1.參數綁定:ajax
a. 提交的數據中有一個對像 的狀況 json
@RequestMapping(value = "/postSave") public String postSave(HttpServletRequest request,Post t) {
b . 提交的數據只有一個參數的狀況
服務器
@RequestMapping(value = "/postPraise/{postId}") public void postPraise(HttpServletRequest request, @PathVariable Long postId)
c . 最後一種就是直接從request對象中獲取app
String newPassword = request.getParameter("password");
二. 返回格式類型(理論上)函數
1. 路徑跳轉post
a . 轉發url
return "home/post"; return "/home/post"; //備註;這個是沒有區別的
b. 重定向spa
return "redirect:/home/post";//項目路徑後 return "redirect:home/post";//當前路徑後 return "redirect:../home/post";//等同於當前路徑 //備註:'/' 表明項目路徑
2 . 返回數據格式code
a. 普通的能夠將數據放到request中
b. 較爲特殊的能夠用json類型
//返回一個字符串 @RequestMapping(value = "/uploadImage") @ResponseBody public Map<String, String> uploadImage(HttpServletRequest request) throws Exception { String imageSrc = "/picture/" + dateFormat.format(new Date()) + "/" + fileName; Map<String, String> modelMap=new HashMap<String,String>(); modelMap.put("imageSrc", imageSrc); return modelMap; }
//返回一個對象 $.ajax({ type:'post', url:'<%=dynamicDomain%>/advert/selectAdvertPosition/'+positionCode, dataType:'json', success:function(msg){ if(msg.result){ alert(msg.advertPosition.width); alert(msg.advertPosition.height); alert("ture"); } } }); @RequestMapping("/selectAdvertPosition/{positionCode}") public String selectAdvertPosition(HttpServletRequest request,@PathVariable String positionCode, ModelMap modelMap) throws IOException{ int width=22; int height=22; AdvertPosition ad= new AdvertPosition (width,height); modelMap.addAttribute("result", true); modelMap.addAttribute("advertPosition", ad);//返回一個對象 return "jsonView"; }
ajax文件上傳
function ajaxFileUpload11() { if($("#uploadFile1").val()==''){ alert('請選擇上傳文件'); return false; } $.ajaxFileUpload({ url: '${dynamicDomain}/screenshot/uploadImage?ajax=1', secureuri: false, fileElementId: 'uploadFile1', //對應文件表單的id dataType: 'json', //服務器返回類型 success: function(json, status) { if(json.result=='true'){ var filePath = json.filePath; } },error: function (data, status, e)//服務器響應失敗處理函數 { alert(e); } } ); return false; } ; @RequestMapping("/uploadImage") public String uploadImage(HttpServletRequest request, HttpServletResponse response, ModelMap map) throws Exception { String result = "false"; //從request流中獲取文件 UploadFile uploadFile = FileUpDownUtils.getUploadFile(request, "uploadFile"); String fileName = uploadFile.getFileName(); if (StringUtils.isNotBlank(fileName)){ if (fileName.endsWith(".jpg") ||fileName.endsWith(".bmp") ||fileName.endsWith(".gif") ||fileName.endsWith(".png") ||fileName.endsWith(".JPG") ||fileName.endsWith(".BMP") ||fileName.endsWith(".GIF") ||fileName.endsWith(".PNG")) { byte[] fileData = FileUpDownUtils.getFileContent(uploadFile.getFile()); String filePath = fileManager.saveImageFile(fileData, uploadFile.getFileName()); BufferedImage image = ImageUtils.readImage(uploadFile.getFile().getAbsolutePath()); result = "true"; map.addAttribute("filePath", filePath.trim()); if (image != null) { map.put("width", image.getWidth() + ""); map.put("height", image.getHeight() + ""); } } else { map.put("message", "圖片格式必須爲jpg/bmp/gif/png"); } } map.put("result", result); return "jsonView"; }