Spring 3 MVC-高级数据绑定-带有简单对象列表的表单请求 [英] Spring 3 MVC - Advanced Data Binding - Form Request with List of Simple Objects
问题描述
I've read through all of the Spring 3 Web docs: http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/spring-web.html but have been completely unable to find any interesting documentation on binding more complicated request data, for example, let's say I use jQuery to post to a controller like so:
$.ajax({
url: 'controllerMethod',
type: "POST",
data : {
people : [
{
name:"dave",
age:"15"
} ,
{
name:"pete",
age:"12"
} ,
{
name:"steve",
age:"24"
} ]
},
success: function(data) {
alert('done');
}
});
我如何通过控制器接受它?最好不必创建自定义对象,我只希望能够使用简单的数据类型,但是如果我需要自定义对象来简化事情,那也可以.
How can I accept that through the controller? Preferably without having to create a custom object, I'd rather just be able to use simple data-types, however if I need custom objects to make things simpler, I'm fine with that too.
让您入门:
@RequestMapping("/controllerMethod", method=RequestMethod.POST)
public String doSomething() {
System.out.println( wantToSeeListOfPeople );
}
不用担心这个问题的答复,我关心的只是处理请求,我知道如何处理答复.
Don't worry about the response for this question, all I care about is handling the request, I know how to deal with the responses.
我有更多的示例代码,但我无法使它正常工作,我在这里错过了什么?
I've got more sample code, but I can't get it to work, what am I missing here?
选择javascript:
select javascript:
var person = new Object();
person.name = "john smith";
person.age = 27;
var jsonPerson = JSON.stringify(person);
$.ajax({
url: "test/serialize",
type : "POST",
processData: false,
contentType : 'application/json',
data: jsonPerson,
success: function(data) {
alert('success with data : ' + data);
},
error : function(data) {
alert('an error occurred : ' + data);
}
});
控制器方法:
public static class Person {
public Person() {
}
public Person(String name, Integer age) {
this.name = name;
this.age = age;
}
String name;
Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
@RequestMapping(value = "/serialize")
@ResponseBody
public String doSerialize(@RequestBody Person body) {
System.out.println("body : " + body);
return body.toString();
}
这将导致以下异常:
org.springframework.web.HttpMediaTypeNotSupportedException: 内容类型"application/json"不是 支持
org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/json' not supported
如果doSerialize()方法采用String而不是Person,则请求成功,但是String为空
If the doSerialize() method takes a String as opposed to a Person, the request is successful, but the String is empty
推荐答案
您的jQuery ajax调用会产生以下application/x-www-form-urlencoded
请求正文(采用%解码形式):
Your jQuery ajax call produces the following application/x-www-form-urlencoded
request body (in %-decoded form):
people[0][name]=dave&people[0][age]=15&people[1][name]=pete&people[1][age]=12&people[2][name]=steve&people[2][age]=24
Spring MVC可以将以数字索引的属性绑定到List
,将以字符串索引的属性绑定到Map
.您在这里需要自定义对象,因为@RequestParam
不支持复杂类型.因此,您有:
Spring MVC can bind properties indexed with numbers to List
s and properties indexed with strings to Map
s. You need the custom object here because @RequestParam
doesn't support complex types. So, you have:
public class People {
private List<HashMap<String, String>> people;
... getters, setters ...
}
@RequestMapping("/controllerMethod", method=RequestMethod.POST)
public String doSomething(People people) {
...
}
您还可以在发送数据之前将数据序列化为JSON,然后按照Bozho的建议使用@RequestBody
.您可以在 mvc-showcase示例中找到此方法的示例. .
You can also serialize data into JSON before sending them and then use a @RequestBody
, as Bozho suggests. You may find an example of this approach in the mvc-showcase sample.
这篇关于Spring 3 MVC-高级数据绑定-带有简单对象列表的表单请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!