spring MVC中传递的参数对象中包含list的情况

解决Spring MVC中复杂对象参数传递问题
本文详细解析了在使用Spring MVC处理复杂对象参数传递时遇到的问题及解决方案,包括JSON转换、Context-Type设置、参数传递方式和类型转换等关键步骤。通过实践案例展示了如何正确配置和使用Spring的JSON消息转换器,确保复杂对象能够顺利传送到后台方法中。

测试需要的jar包:spring 3.2.jar +  jackson-all-1.8.5.jar。

写代码时碰到个需要将对象里的子明细一起传递到controller里去,当时就想直接将参数一起传递过来,贴下代码:

controller:

  1. @RequestMapping(params="add")  
  2. @ResponseBody  
  3. public CustomForeignKey add(@RequestBody CustomForeignKey customForeignKey,Model model,ModelAndView mv,HttpServletRequest req){  
  4.     return customForeignKeyService.add(customForeignKey);  
  5. }  

参数对象:
  1. public class CustomForeignKey {  
  2.     private Long id;  
  3.     private Long tableId;  
  4.     private Long foreignKeyTableId;  
  5.     private String foreignKeyTableName;  
  6.     private String name;  
  7.     private List<CustomForeignKeyRelation> customForeignKeyRelations;  
  8.       
  9.     public Long getId() {  
  10.         return id;  
  11.     }  
 对象下子明细:CustomForeignKeyRelation

  1. public class CustomForeignKeyRelation {  
  2.     private Long id;  
  3.     private Long customForeignKeyId;  
  4.     private Long leftCustomColumnId;  
  5.     private String leftCustomColumnName;  
  6.     private String leftCustomColumnAlias;  
  7.     private Long rightCustomColumnId;  
  8.     private String rightCustomColumnName;  
  9.     private String rightCustomColumnAlias;  
  10.       
  11.     public Long getId() {  
  12.         return id;  
  13.     }  

js传递的代码段:

[javascript] view plain copy
  1. var relations = [];  
  2. $.each(rows,function(i,obj){  
  3.     if(obj.leftCustomColumnId != 0 && obj.leftCustomColumnName && obj.leftCustomColumnName != '无')  
  4.         relations.push({leftCustomColumnId:obj.leftCustomColumnId,  
  5.                         leftCustomColumnName:obj.leftCustomColumnName,  
  6.                         rightCustomColumnId:obj.rightCustomColumnId,  
  7.                         rightCustomColumnName:obj.rightCustomColumnName});  
  8. })  
  9.   
  10. var dd = {tableId:selectRowOfTable.id,  
  11.         name:t_fk_name,  
  12.         foreignKeyTableId:$("#foreignKeyDialog_foreignTableId").combobox("getValue"),  
  13.         foreignKeyTableName:$("#foreignKeyDialog_foreignTableId").combobox("getText"),  
  14.         customForeignKeyRelations:relations  
  15.         };  
  16. /*$.post("customForeignKey.htm?add",dd,function(){ 
  17.     dgForeignKey.datagrid("reload"); 
  18.     foreignKeyDialog.dialog("close"); 
  19. });*/  
  20. $.ajax({  
  21.     url:"customForeignKey.htm?add",  
  22.     type:"post",  
  23.     dataType:"json",  
  24.     contentType:"application/json",  
  25.     data:JSON.stringify(dd),  
  26.     success:function(){  
  27.         alert("success");  
  28.     }  
  29. });  

按照网上所说,我将  ajax的 contentType:"application/json",将json对象改成json字符,在参数前面加上@RequestBody,可是传递到add方法里之后,却发现还是个空,不仅里面的list没值,连那些 long、String类型的也都没有值,后经过几个小时的研究发现,原来是配置spring 的json的类型里少写了“application/json;charset=UTF-8”,导致没有应用到json的类型转换。

  1. <bean id="mappingJacksonHttpMessageConverter"  
  2.     class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">  
  3.     <property name="supportedMediaTypes">  
  4.         <list>  
  5.             <value>text/html;charset=UTF-8</value>  
  6.             <value>application/json;charset=UTF-8</value>  
  7.         </list>  
  8.     </property>  
  9. </bean>  
加上这个之后就ok了。


现在总结一下 springMVC传递参数的的方法。

1.ajax:   传递的参数请求头里  Context-Type 的问题,我们若是用$.post的话,它默认是为application/x-www-from-urlencoded;charset=utf-8,网上说要改为“application/json”才能转换,实际上你改成其他的也行,只要在xml的配置文件里包含它,那么它将会在转换时会采用json包下的 MappingJacksonHttpMessageConverter类进行转换;网上有人说spring只支持简单类型的数组转换,我想就是因为他们没有配置MappingJacksonHttpMessageConverter类的使用,MappingJacksonHttpMessageConverter不属于spring。

2.参数:平常我们使用访问时,是直接将json对象传递给后台,而当我们的对象里包括数组时,就不能使用json对象,在jquery里,post里的json对象会转换成url参数模式传递,里面的对象的[、]、.也会被转换成其他%%之类的,这种情况下spring 会将它们当做简单的key=value值进行转换,是不会进入到MappingJacksonHttpMessageConverter类中,  此时转换时会报错, 说 customForeignKeyRelations[0].id  没有对应属性,它们将 customForeignKeyRelations[0].id当成一个属性来转换;按照MappingJacksonHttpMessageConverter的要求,我们需要将json对象改为字符串传递,可以使用 JSON.stringify(jsonData)方法将json对象转换成字符串(该方法在ie低级版本里没有),此时我们在request的参数里可以看到它与我们平常传递的参数的不同。


太啰嗦了,简单点:

1.定义Context-Typeapplication/json;charset=utf-8: ajax传递参数里更改,spring配置文件里 添加;

2.json对象传递时,改为字符串,通用方法为JSON.stringify(jsonData);

3.接收字符串对象:在参数前加 @RequestBody
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值