SpringMVC格式总结

  1. SpringMVC几种数据接收方式java

    1.参数绑定:ajax

       a. 提交的数据中有一个对像 的状况   json

  2. @RequestMapping(value = "/postSave")
        public String postSave(HttpServletRequest request,Post t) {

       b . 提交的数据只有一个参数的状况
    服务器

  3. @RequestMapping(value = "/postPraise/{postId}")
    	public void postPraise(HttpServletRequest request, @PathVariable Long postId)

     c . 最后一种就是直接从request对象中获取app

  4. 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";
	}   
	    
	    
相关文章
相关标签/搜索