java Spring 3.0 MVC Ajax 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3958387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring 3.0 MVC Ajax example
提问by danny.lesnik
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?
我究竟做错了什么?
回答by axtavt
There are two possible reasons:
有两个可能的原因:
- You forget
<mvc:annotation-driven />
. It automatically configures HTTP message converters for use with@RequestBody
/@ResponseBody
- You don't have Hymanson JSON Processorin the classpath. Spring requires it to bind
application/json
to@RequestBody
- 你忘了
<mvc:annotation-driven />
。它会自动配置 HTTP 消息转换器以用于@RequestBody
/@ResponseBody
- 您的类路径中没有Hymanson JSON 处理器。Spring 要求它绑定
application/json
到@RequestBody
回答by hisdrewness
Just a couple of other helpful links...check out this Spring blog post:
只是一些其他有用的链接……查看这篇 Spring 博客文章:
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:
@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”。
回答by Sarel Botha
This is not exactly an answer to your question, but have you looked at DWR before? It makes JS to Java RPC super-easy. http://directwebremoting.org/dwr/index.html
这不完全是您问题的答案,但是您以前看过 DWR 吗?它使 JS 到 Java RPC 变得超级简单。http://directwebremoting.org/dwr/index.html