spring MVC中传递的参数对象中包含list的情况
2017-02-20 16:13
381 查看
原址:http://blog.csdn.net/xiaohua914/article/details/9925619
写代码时碰到个需要将对象里的子明细一起传递到controller里去,当时就想直接将参数一起传递过来,贴下代码:
controller:
[java] view
plain copy
@RequestMapping(params="add")
@ResponseBody
public CustomForeignKey add(@RequestBody CustomForeignKey customForeignKey,Model model,ModelAndView mv,HttpServletRequest req){
return customForeignKeyService.add(customForeignKey);
}
参数对象:
[java] view
plain copy
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
[java] view
plain copy
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传递的代码段:
[javascript] view
plain copy
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的类型转换。
[html] view
plain copy
<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
=======================================================================
$(document).on('click', '.save', function (e) {
var customPlansList =[];
$("#formBind").find(".data_cells").each(function(i){
var _this = $(this);
customPlansList.push({
startTime:_this.find("input[name=startTime]").val(),
startUnit:_this.find("input[name=startUnit]").val(),
endTime:_this.find("input[name=endTime]").val(),
endUnit:_this.find("input[name=endUnit]").val(),
loadWorking:_this.find(".loadWorking").find("input:checked").val(),
kneePractice:_this.find("input[name=kneePractice]").val(),
braceScope:_this.find("input[name=braceScope]").val(),
});
});
var plan = {operationTime:$("input[name=operationTime]").val(),
position:$("input[name=position]:checked").val(),
surgicalType:$("input[name=surgicalType]:checked").val(),
propType:$("input[name=propType]:checked").val(),
customPlans:customPlansList
};
$.ajax({
dataType:"json",
contentType:"application/json;charset=utf-8",
url: "<%=request.getContextPath()%>/recovery/createPlan.do",
data:JSON.stringify(plan),
type:"post",
beforeSend: function() {
/* $("#inputData").addClass("btn-default").unbind("click"); */
},
success:function(data){
swal({
title: "创建成功!",
type: "success",
showCancelButton: false,
confirmButtonColor: "#8cd4f5",
confirmButtonText: "确定"
}, function (isConfirm) {
window.location.href="<%=request.getContextPath()%>/Views/recovery/recoveryDetail.jsp";
}, "success");
},
complete: function(data) {
},error:function(){
swal("操作失败!", "", "error");
}
});
});
controller
public Map<String, Object> createPlan(HttpServletRequest request, @RequestBody RecoveryPlan plan)
写代码时碰到个需要将对象里的子明细一起传递到controller里去,当时就想直接将参数一起传递过来,贴下代码:
controller:
[java] view
plain copy
@RequestMapping(params="add")
@ResponseBody
public CustomForeignKey add(@RequestBody CustomForeignKey customForeignKey,Model model,ModelAndView mv,HttpServletRequest req){
return customForeignKeyService.add(customForeignKey);
}
参数对象:
[java] view
plain copy
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
[java] view
plain copy
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传递的代码段:
[javascript] view
plain copy
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的类型转换。
[html] view
plain copy
<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
=======================================================================
$(document).on('click', '.save', function (e) {
var customPlansList =[];
$("#formBind").find(".data_cells").each(function(i){
var _this = $(this);
customPlansList.push({
startTime:_this.find("input[name=startTime]").val(),
startUnit:_this.find("input[name=startUnit]").val(),
endTime:_this.find("input[name=endTime]").val(),
endUnit:_this.find("input[name=endUnit]").val(),
loadWorking:_this.find(".loadWorking").find("input:checked").val(),
kneePractice:_this.find("input[name=kneePractice]").val(),
braceScope:_this.find("input[name=braceScope]").val(),
});
});
var plan = {operationTime:$("input[name=operationTime]").val(),
position:$("input[name=position]:checked").val(),
surgicalType:$("input[name=surgicalType]:checked").val(),
propType:$("input[name=propType]:checked").val(),
customPlans:customPlansList
};
$.ajax({
dataType:"json",
contentType:"application/json;charset=utf-8",
url: "<%=request.getContextPath()%>/recovery/createPlan.do",
data:JSON.stringify(plan),
type:"post",
beforeSend: function() {
/* $("#inputData").addClass("btn-default").unbind("click"); */
},
success:function(data){
swal({
title: "创建成功!",
type: "success",
showCancelButton: false,
confirmButtonColor: "#8cd4f5",
confirmButtonText: "确定"
}, function (isConfirm) {
window.location.href="<%=request.getContextPath()%>/Views/recovery/recoveryDetail.jsp";
}, "success");
},
complete: function(data) {
},error:function(){
swal("操作失败!", "", "error");
}
});
});
controller
public Map<String, Object> createPlan(HttpServletRequest request, @RequestBody RecoveryPlan plan)
相关文章推荐
- spring MVC中传递的参数对象中包含list的情况
- spring MVC中传递的参数对象中包含list的情况
- spring MVC中传递的参数对象中包含list的情况
- mybatis的mapper接口方法参数是对象,对象中包含list情况的处理
- 关于如何处理Mybatis参数为对象中包含list情况
- Mybatis参数为对象中包含list情况处理
- Spring MVC 作业过程中传递参数为集合List的解决方案
- 利用AJAX为JSP页面传递一个包含了若干对象的List集合
- Spring MVC绑定 List 对象参数
- 利用AJAX为JSP页面传递一个包含了若干对象的List集合
- 参数传递数组 接收 前端使用jquery向后台传递数组类型的参数,java后台直接通过list类型接收,会发现无法取到参数的情况。
- 通过参数传递,判断数字、字符串、list、tuple、词典等数据类型是否为可变数据对象。
- AXIS传递包含javaBean对象的List对象
- spring mvc 如何从前台表单传递集合参数并绑定集合对象。 [问题点数:40分,结帖人NewMoons]
- spring MVC中传递对象参数示例详解
- spring mvc 如何传递集合参数(list,数组)
- spring mvc 传递 list 对象
- spring mvc 如何传递集合参数(list,数组)
- Spring MVC中如何传递对象参数
- boost::bind绑定成员函数时,第一个参数传递对象的特殊情况