[英]Spring 3.0 MVC Ajax example
I'm trying to send an Ajax request to a Spring MVC controller and map it to a Java class accordingly: 我正在尝试将Ajax请求发送到Spring MVC控制器,并将其相应地映射到Java类:
public class Person implements Serializable {
private MutableLong Id = new MutableLong();
@NotEmpty
@Size(min = 1, max = 50)
String FirstName=null;
@NotEmpty
@Size(min = 1, max = 50)
String LastName=null;
public Person(){}
public long getId(){
return this.Id.longValue();
}
//getters and setters
}
then I have JavaScript which sends the AJAX request: 然后我有发送AJAX请求的JavaScript:
function loadXMLDoc(){
if(window.ActiveXObject)
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
xmlHttp.onreadystatechange=handleStateChange;
xmlHttp.open("POST","/authenticate.dlp", true);
xmlHttp.setRequestHeader('Content-Type', 'application/json');
param = '{\"FirstName\"=\"test\",\"LastName\"=\"test2\"}';
xmlHttp.send(param);
}
and then the controller itself: 然后控制器本身:
@RequestMapping(value="/authenticate.dlp",method = RequestMethod.POST)
@ResponseBody
public String getAjax(@RequestBody Person person){
Set<ConstraintViolation<Person>> failures = validator.validate(person);
if(!failures.isEmpty())
//......
}
It looks like no response from the server. 服务器似乎没有响应。 If I'm using Fiddler, I see the following response from the server:
如果使用的是Fiddler,则会从服务器看到以下响应:
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method ().
服务器拒绝了此请求,因为请求实体的格式不受请求方法()的请求资源支持。
What am I doing wrong? 我究竟做错了什么?
There are two possible reasons: 有两个可能的原因:
<mvc:annotation-driven />
. <mvc:annotation-driven />
。 It automatically configures HTTP message converters for use with @RequestBody
/ @ResponseBody
@RequestBody
/ @ResponseBody
application/json
to @RequestBody
application/json
绑定到@RequestBody
Just a couple of other helpful links...check out this Spring blog post: 其他几个有用的链接...请查看此春季博客文章:
http://blog.springsource.com/2010/07/22/spring-mvc-3-showcase/ http://blog.springsource.com/2010/07/22/spring-mvc-3-showcase/
And the examples which make use of @ResponseBody: 以及使用@ResponseBody的示例:
https://src.springframework.org/svn/spring-samples/mvc-showcase/ https://src.springframework.org/svn/spring-samples/mvc-showcase/
There's also ResponseEntity: 还有ResponseEntity:
http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/ResponseEntity.html http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/http/ResponseEntity.html
@RequestMapping("/ajax/helloworld")
public ResponseEntity<String> helloworld() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<String>("Hello World", headers, HttpStatus.OK);
}
Where instead of "Hello World" you could return a marshalled object. 除了“ Hello World”,您还可以返回一个编组的对象。
This is not exactly an answer to your question, but have you looked at DWR before? 这不完全是您问题的答案,但是您以前看过DWR吗? It makes JS to Java RPC super-easy.
它使JS到Java RPC变得非常容易。 http://directwebremoting.org/dwr/index.html
http://directwebremoting.org/dwr/index.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.