您的位置:首页 > 编程语言 > Java开发

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

2017-04-17 10:38 309 查看
测试需要的jar包:spring 3.2.jar + jackson-all-1.8.5.jar。

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

controller:

@RequestMapping(params="save")
@ResponseBody
public CustomForeignKey save(@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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: