JavaWeb——springMVC请求数据绑定分类解析(深度好文)

来源:互联网 发布:网络实名制 郭德纲 编辑:程序博客网 时间:2024/04/18 17:14

一、引言


      最早我使用的是WebAPI微软的后台,把所有的数据绑定请求方法全部总结了一个遍,基本有了全面的认识。如今转战到java的SpringMVC平台,在此又把所有的方法总结一遍,算是springMVC版本,供大家参考了==

      本章框架如下:



二、GET数据绑定


  • 简单类型


前台请求:


http://localhost:8080/SpringMVCmyBatis/user/paramGet1?id=123&name=阿道夫


后台接收代码:(其中id与name分别可以接受到数据)


@RequestMapping(value="/paramGet1", method=RequestMethod.GET)@ResponseBody//requestParam中value指参数别名,required为是否必须,defaultvalue为参数默认值public String paramGet1(@RequestParam(value="id",required=true,defaultValue="666") Integer ididid,String name) throws Exception {return ididid+name;}


  • pojo类型


前台请求:


http://localhost:8080/SpringMVCmyBatis/user/paramGet2?f_id=123&f_code=阿道夫


后台接收代码:(其中user可以接收到f_id与f_code参数)


//value请求路径,method请求方式@RequestMapping(value="/paramGet2", method=RequestMethod.GET)@ResponseBodypublic String paramGet2(User user,Integer F_ID) throws Exception {return user.getF_id()+user.getF_code();}

User类

public class User {private int f_id;private String f_code;private String f_pw;private Department department;public int getF_id() {return f_id;}public void setF_id(int f_id) {this.f_id = f_id;}public String getF_code() {return f_code;}public void setF_code(String f_code) {this.f_code = f_code;}public String getF_pw() {return f_pw;}public void setF_pw(String f_pw) {this.f_pw = f_pw;}public Department getDepartment() {return department;}public void setDepartment(Department department) {this.department = department;}}


三、POST数据绑定


1、application/json


  • 简单类型(该种方法证明失败不能成功接收)

前台请求:



后台接收:
@RequestMapping(value="/paramPostJson1", method=RequestMethod.POST)public @ResponseBody String paramPostJson1(String F_ID,String F_CODE) throws Exception {return F_ID+F_CODE;}

  • pojo类型


前台请求:



后台接收:(记得要添加@ResponseBody)

@RequestMapping(value="/paramPostJson2", method=RequestMethod.POST)public @ResponseBody User paramPostJson2(@RequestBody User user) throws Exception {return user;}



2、application/x-www-form-urlencoded


  • 简单类型

前台请求:


后台接收:
@RequestMapping(value="/paramPostNormalUrlencode1", method=RequestMethod.POST)@ResponseBodypublic String paramPostNormalUrlencode1(String F_ID,String F_CODE) throws Exception {return F_ID+F_CODE;}

  • pojo类型

前台请求:



后台接收:(记得不要添加@ResponseBody,否则接收不到)

@RequestMapping(value="/paramPostNormalUrlencode2", method=RequestMethod.POST)@ResponseBodypublic String paramPostNormalUrlencode2(User user) throws Exception {return user.getF_id()+user.getF_code();}



3、multipart/form-data


  • 简单类型

前台请求:



后台接收:

@RequestMapping(value="/paramPostFormdata1", method=RequestMethod.POST)@ResponseBodypublic String paramPostFormdata1(HttpServletRequest request, String F_ID,String F_CODE) throws Exception {String id1 = request.getParameter("F_ID");String code1 = request.getParameter("F_CODE");return id1+code1;//return F_ID+F_CODE;}

  • pojo类型

前台请求:



后台接收:

@RequestMapping(value="/paramPostFormdata2", method=RequestMethod.POST)@ResponseBodypublic String paramPostFormdata2( User user) throws Exception {return user.getF_id()+user.getF_code();}




      很有可能你会接收不到,那是因为springMVC使用formdata方式要配置MultipartResolver ,在springmvc配置文件中添加

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">    <property name="defaultEncoding" value="utf-8"></property>    <property name="maxUploadSize" value="10485760000"></property>    <property name="maxInMemorySize" value="40960"></property></bean>

然后还可能接着报错,这个就比较好找了,因为springMVC要用到file-upload jar包,而这个jar包同时依赖commons-iob jar包,说白了就是缺jar包,添加上就好了==bingo



四、总结


  • dot net webAPI与java springMVC

  • get中的简单类型与pojo类型绑定

  • post中enctype为application/json、application/x-www-form-urlencoded、multipart/form-data时的简单类型与pojo类型数据绑定;

  • post multipart/form-data方法配置



阅读全文
0 0