【发布时间】:2015-06-25 06:15:42
【问题描述】:
我遇到的问题是标题中提到的,也尝试在 stackoverflow 上搜索,但我没有任何解决方案。
我有一个Jsp页面
<!DOCTYPE html>
<html>
<head>
<title>CreateAccount</title>
<script src="https://ajax.googleapis.com/ajax/libs/ext-core/3.1.0/ext-core.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
var delay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$(document).ready(function(){
$("input.uname").keyup(function() {
delay(function(){
//alert('Hi, func called');
checkUsername();
}, 1000 );
});
});
function checkUsername() {
alert('go');
var requrl = "http://localhost:8082/VendorWebApplication/reqq3";
var greeting = {
"id" : 1,
"content" :"prasad"
}
$.ajax({
type: 'POST',
url: requrl,
data:JSON.stringify(greeting),
dataType: "text",
//dataType: "json", //tried with this also
contentType: 'application/json',
success: function (data) {
debugger;
var json = $.parseJSON(data);
alert(json.content);
},
error: function (data) {
debugger;
alert("error"+data);
}
});
}
</script>
</head>
<body>
//body content
</body>
</html>
我想发送 Id,Content 到 spring 控制器
这是我的控制器类
@Controller
public class WebController {
@RequestMapping(value ="/reqq3",method=RequestMethod.POST)
public @ResponseBody Greeting reqq3(@RequestBody Greeting greeting) {
System.out.println("omgcheck");
System.out.println(greeting.getContent());
System.out.println(greeting.getId());
return new Greeting(1,"checkboss");
}
}
但是在这里我的 ajax 请求甚至没有命中
我的 web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring MVC Application</display-name>
<servlet>
<servlet-name>VendorWebApplication</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>VendorWebApplication</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
我的 VendorWebApplication-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.arsenal.vendorappserver.controller"/>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jsonConverter" />
</list>
</property>
</bean>
<bean id="jsonConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes" value="application/json"></property>
</bean>
我的 Greeting.class
public class Greeting {
private final long id;
private final String content;
public Greeting(long id, String content) {
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
让我告诉你,当我发送带有数据的 GET 请求时,它会击中控制器并捕获数据(使用方法 = RequestMethod.GET 和所有)。但是当我请求 method=POST 时, 没有击中。
【问题讨论】:
标签: spring spring-mvc controller