[英]How can I send objects to Spring by GET request?
I am using Spring MVC 3 and I want to make an AJAX call sending an array of objects to a controller. 我正在使用Spring MVC 3,并且想进行AJAX调用,将对象数组发送到控制器。 I want my Java to look like this:
我希望我的Java看起来像这样:
@RequestMapping (value = "/data/save/faults" method = RequestMethod.GET)
public void saveFaultsGET
(
@RequestParam ("faults") FaultType[] types
) { }
Note the FaultType
is an object I wrote. 注意
FaultType
是我编写的对象。 I am using jQuery and I am not sure how I would format my request URL to achieve this. 我正在使用jQuery,但不确定如何格式化请求URL来实现此目的。 It would also be helpful to know if it's not possible.
知道是否可行也是有帮助的。
EDIT FaultType
looks like this (getters and setters omitted): EDIT
FaultType
看起来像这样(省略了getter和setter的方法):
public class FaultType {
private String m_type;
private boolean m_isTrip;
private boolean m_isRelay;
private boolean m_isNonRelay;
}
This is very good intro : http://blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/ 这是很好的介绍: http : //blog.springsource.com/2010/01/25/ajax-simplifications-in-spring-3-0/
The ajax call should look something like this: ajax调用应如下所示:
<c:url var="myURL" value="/data/save/faults" />
...
jq.ajax({
cache:false,
type: 'POST',
url: "${myURL}",
data:jsonFaultType,
contentType: "application/json",
success: function(data) {
// yourFunction to call on success eg:
alert(data);
}
});
Your controller signature should look something like this: 您的控制器签名应如下所示:
@RequestMapping (value = "/data/save/faults" method = RequestMethod.POST)
public @ResponseBody String create(@RequestBody FaultType faultType){
...
}
Why do you want to send objects using a get request, POST is the defacto method for posting data to server . 为什么要使用get请求发送对象,POST是将数据发布到服务器的事实上的方法。 Also, you don't have to return a responsebody, you can also return a modelAndView .
另外,您不必返回响应主体 ,也可以返回modelAndView 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.