如何将@RequestParam绑定到spring MVC中的object? [英] How to bind @RequestParam to object in spring MVC?
问题描述
我想通过AJAX发出POST请求,我还想将整个类对象绑定到请求,我想用 @requestParam
接收该请求注解。我知道可以用 @requestBody
注释完成,但我很想知道:我们可以用 @requestParam
注释?
I want to make a POST request through AJAX, and I also want to bind the whole class object to the request, and I want to receive that request with @requestParam
annotation. I know it can be done with @requestBody
annotation, but I am curious to know: can we do it with @requestParam
annotation?
Ajax代码:
var restDTO{
id: 3,
name: "hello"
}
$.ajax({
url: url,
type: 'POST',
dataType: 'json',
contentType: 'application/json',
mimeType: 'application/json',
data: JSON.stringify({RestDTO : restDTO}),
success: function(data)
{
}
我确实有RestDTO
I do have RestDTO
Class RestDTO
{
int id;
String name;
//getter and setter
}
在控制器中
public String content(@RequestParam RestDTO restDTO){...}
我该怎么做此代码运行?
What should I do the make this code run?
从ajax发送数据应该更改什么?
What should I change in sending data from ajax?
我是否需要更改服务器接收带有 @requestParam
注释的RestDto对象?
Do I need to change on server to receive an RestDto object with @requestParam
annotation?
推荐答案
你不能,因为 @RequestParam
只是表示,一个方法的参数应绑定到一个 Web请求的参数。它无法映射到对象。要使用 @RequestParam
,您应该更改ajax请求:
You can't, because @RequestParam
just indicates, that one method's parameter should be bound to a one web request's parameter. It can't do mapping to objects. For use @RequestParam
you should change ajax request:
var restDTO{
id: 3,
name: "hello"
}
$.ajax({
url: url,
type: 'POST',
data: restDTO,
success: function(data){
....
}
});
JQuery将发送请求为 application / x-www-form-urlencoded
并将自动处理数据到参数。你的控制器的方法应如下所示:
JQuery will send request as application/x-www-form-urlencoded
and will process data to parameters automatically. You controller's method should look like following:
@RequestMapping("/url")
public String content(@RequestParam Long id, @RequestParam String name){...}
为了自动将参数映射到对象,你可以使用 @ModelAttribute
注释:
For automatically map parameters to object you can use @ModelAttribute
annotation:
@RequestMapping("/url")
public String content(@ModelAttribute RestDTO restDTO){...}
在这种情况下,javascript地图中的名称应与 RestDTO
中的属性名称匹配。
In this case, names in javascript map should match to names of properties in RestDTO
.
通常,为相同目的创建的@ModelAttribute
和 @RequestBody
:用于将数据从请求绑定到方法(是否为基本类型的对象)。
Generally, @ModelAttribute
and @RequestBody
created for same purposes: for binding data from request to method (whether objects of primitive type).
我认为,当你使用html-forms和普通对象时, @ModelAttribute
会更方便。可以使用Spring功能,例如 modelAttribute
和路径
。
I consider, that @ModelAttribute
is more convenient, when you working with html-forms and plain objects. There is ready to use Spring abilities like modelAttribute
and path
.
当您需要手动控制数据时, @RequestBody
更灵活。此外,当您使用复杂对象时,它会更方便。
In its turn, @RequestBody
more flexible, when you need manual control over data. Also, it is more convenient, when you're working with complex objects.
我个人更喜欢 @RequestBody
和json。
Personally me would prefer @RequestBody
and json.
这篇关于如何将@RequestParam绑定到spring MVC中的object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!