Controller层接收各种参数和文件
时间: 2018-04-17来源:OSCHINA
前景提要
【围观】麒麟芯片遭打压成绝版,华为亿元投入又砸向了哪里?>>>
在构建一个系统时,前端和后台总是需要对接,在springmvc架构里,这种对接一般发生在Controller层中。方法参数绑定首先支持Java所有基本类型(包括: byte、short、int、long、float、double、char、string、boolean),以及基本类型对应封装高级类(包括:StringBuilder、StringBuffer),也包含 了我们自己定义的各种JavaBean类型。接受的方式有很多,但是也要在接受数据时也要体现面向对象的思想。
一、简单传入
以用户登陆为例,在前台传入数据,做一个传入的例子。
前台传入数据的形式为 username=10&password=10 <html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> //简单形式的传参 $.ajax({ type: "POST", url: "http://localhost:8080/test/requesetParamGet", contentType: "application/x-www-form-urlencoded", data:"username=10&password=10", dataType: "json", success: function (result) { if (result.code == 0) { console.log(result) } else { } } }); </script> </head> </html>
在这种情况下,如何在后台进行接受呢?可以采用如下的方法。 @ResponseBody @RequestMapping("/simple") public R list(String name,String age){ System.out.println("name:"+name+",age:"+age); return R.ok(); }
当然也可以采用HttpServeletRequest的形式进行接受 @ResponseBody @RequestMapping("/simple") public R requestGet(HttpServletRequest request){ System.out.println("reqname:"+request.getParameter("name")+",reqage:"+request.getParameter("age")); return R.ok(); }
二、以json对象的形式传入
还是以用户登陆为例,在前台传入数据,做一个传入的例子。 <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> //class获取,需要用对象的形式 var s={ name:'liMin', age:'10' } $.ajax({ type: "POST", url: "http://localhost:8080/test/classGet", contentType: "application/json", data:JSON.stringify(s), dataType: "json", success: function (result) { if (result.code == 0) { console.log(result) } else { } } }); </script>
这种情况下,controller层建议使用对象的形式进行接受。 @ResponseBody @RequestMapping("/classGet") public R classGet(@RequestBody TestEntity testEntity){ System.out.println("className:"+testEntity.getName()+"classAge:"+testEntity.getAge()); return R.ok(); }
在传参时,需要添加注解@RequsetBody用来接收contentType为application/json的传入对象。如果传过来的时contentType头为application/x-www-form-urlencoded,那么建议使用另外一个注解接受@RequestParam来接受。 @RequestMapping("/requesetParamGet") public R addUser6(@RequestParam("username") String username,@RequestParam("password") String password) { System.out.println("username is:"+username); System.out.println("password is:"+password); return R.ok(); }
如果啥都不写,那么就得不到这个对象,接受到的对象为NULL。
当然,你可以不接受为一个对象,可以把传过来的json对象转化为json字符串,然后用各种工具进行解析,也是可以的。当然也是要加上@RequestBody或者@RequestParam的。 @ResponseBody @RequestMapping("/stringGet") public R stringGet(@RequestBody String string){ System.out.println("String:"+string); return R.ok(); }
三、文件传输
在项目中,文件上传有别于对象的上传。 <html> <head> <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script> <script> function savePic(){ var formData = new FormData($( "#uploadPic" )[0]); var ajaxUrl = "http://localhost:8080/test/fileUpload"; //alert(ajaxUrl); //$('#uploadPic').serialize() 无法序列化二进制文件,这里采用formData上传 //需要浏览器支持:Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。 $.ajax({ type: "POST", //dataType: "text", url: ajaxUrl, data: formData, //async: false, //cache: false, contentType: false, //上传文件 processData: false,//序列化处理,默认为true,上传文件需要改成false success: function (data) { alert(data); }, error: function(data) { alert("error:"+data.responseText); } }); return false; } function jiance(){ var formData = new FormData(); formData.append() } </script> </head> <body> <form id="uploadPic" action="" enctype="multipart/form-data"> <input type="file" name="multipartFile" id="file"> <a href="javascript:savePic();" class="btn green"> 提交 </a> <a href="javascript:jiance();" class="btn green"> jiance </a> </form> </body> </html>
在后台接受参数的例子: @RequestMapping("/fileUpload") @ResponseBody public R upload(MultipartFile multipartFile){ String filePath=""; if(!multipartFile.isEmpty()){ System.out.println(multipartFile.getOriginalFilename()); } return R.ok().put("filePath",filePath); } @RequestMapping("/fileUpload2") @ResponseBody public R upload2(@RequestParam("multipartFile") MultipartFile multipartFile){ String filePath=""; if(!multipartFile.isEmpty()){ System.out.println(multipartFile.getOriginalFilename()); } return R.ok().put("filePath",filePath); } @RequestMapping("/fileUpload3") @ResponseBody public R upload3(@RequestBody MultipartFile multipartFile){ String filePath=""; if(!multipartFile.isEmpty()){ System.out.println(multipartFile.getOriginalFilename()); } return R.ok().put("filePath",filePath); }
这里需要注意一点,文件名必须和参数名保持一致,在本项目中file文件的名字必须为multipartFile。

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行