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

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

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

controller:

	
	@RequestMapping(params="add")
	@ResponseBody
	public 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

  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值