spring MVC中传递的参数对象中包含list的情况
来源:互联网 发布:雕刻机编程 编辑:程序博客网 时间:2024/04/29 11:42
测试需要的jar包:spring 3.2.jar + jackson-all-1.8.5.jar。
写代码时碰到个需要将对象里的子明细一起传递到controller里去,当时就想直接将参数一起传递过来,贴下代码:
controller:
@RequestMapping(params="add")@ResponseBodypublic CustomForeignKey add(@RequestBody CustomForeignKey customForeignKey,Model model,ModelAndView mv,HttpServletRequest req){return customForeignKeyService.add(customForeignKey);}
参数对象:
public class CustomForeignKey {private Long id;private Long tableId;private Long foreignKeyTableId;private String foreignKeyTableName;private String name;private List<CustomForeignKeyRelation> customForeignKeyRelations;public Long getId() {return id;}对象下子明细:CustomForeignKeyRelation
public class CustomForeignKeyRelation {private Long id;private Long customForeignKeyId;private Long leftCustomColumnId;private String leftCustomColumnName;private String leftCustomColumnAlias;private Long rightCustomColumnId;private String rightCustomColumnName;private String rightCustomColumnAlias;public Long getId() {return id;}
js传递的代码段:
var relations = [];$.each(rows,function(i,obj){if(obj.leftCustomColumnId != 0 && obj.leftCustomColumnName && obj.leftCustomColumnName != '无')relations.push({leftCustomColumnId:obj.leftCustomColumnId,leftCustomColumnName:obj.leftCustomColumnName,rightCustomColumnId:obj.rightCustomColumnId,rightCustomColumnName:obj.rightCustomColumnName});})var dd = {tableId:selectRowOfTable.id,name:t_fk_name,foreignKeyTableId:$("#foreignKeyDialog_foreignTableId").combobox("getValue"),foreignKeyTableName:$("#foreignKeyDialog_foreignTableId").combobox("getText"),customForeignKeyRelations:relations};/*$.post("customForeignKey.htm?add",dd,function(){dgForeignKey.datagrid("reload");foreignKeyDialog.dialog("close");});*/$.ajax({url:"customForeignKey.htm?add",type:"post",dataType:"json",contentType:"application/json",data:JSON.stringify(dd),success:function(){alert("success");}});
按照网上所说,我将 ajax的 contentType:"application/json",将json对象改成json字符,在参数前面加上@RequestBody,可是传递到add方法里之后,却发现还是个空,不仅里面的list没值,连那些 long、String类型的也都没有值,后经过几个小时的研究发现,原来是配置spring 的json的类型里少写了“application/json;charset=UTF-8”,导致没有应用到json的类型转换。
<bean id="mappingJacksonHttpMessageConverter"class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"><property name="supportedMediaTypes"><list><value>text/html;charset=UTF-8</value><value>application/json;charset=UTF-8</value></list></property></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
- spring MVC中传递的参数对象中包含list的情况
- spring MVC中传递的参数对象中包含list的情况
- spring MVC中传递的参数对象中包含list的情况
- mybatis的mapper接口方法参数是对象,对象中包含list情况的处理
- Spring MVC中如何传递对象参数
- Spring MVC中如何传递对象参数
- Spring MVC中如何传递对象参数
- Mybatis参数为对象中包含list情况处理
- Spring MVC 作业过程中传递参数为集合List的解决方案
- Hadoop中传递list/Map/对象型参数的传递
- 关于如何处理Mybatis参数为对象中包含list情况
- spring mvc中登录用户信息的传递
- spring mvc 传递 list 对象
- mybatis复合参数 map中包含list的参数
- Spring MVC中通过Jquery低版本的ajax传递参数值 遇到415的错误
- 多线程中MFC对象参数的传递
- Spring MVC中action接收从页面传递过来的参数方式
- MVC 中前端传递Json数据,后端以对象形式接受参数的处理方法
- Tomcat 后台开机启动配置
- IBM服务器x3650的DASD灯常亮
- div-css布局:新闻列表过长溢出加…方法
- HDOJ 4661: Message Passing
- linux 下的解压缩、分卷、加密
- spring MVC中传递的参数对象中包含list的情况
- html CSS 阴影边框
- linux下svn命令
- mysql 添加外键
- math.h中的函数
- 俞敏洪:生命的远行不需要太多的准备
- VMware vSphere5.0 学习之手动克隆虚拟机
- 注意python dict的内容变化
- 直接内存访问(DMA)