我有一個從JQuery.post()調用的彈簧控制器。當它被調用時,控制器的方法被調用並返回。但是,在後臺,Spring會更改URL並調用服務器增益。服務器響應與404彈簧控制器404在POST方法調用後重新調用
我認爲這是爲了應對春節試圖找到一個查看POST方法已被處理後。
如何阻止Spring控制器執行此操作。
這裏是我的春天控制器:
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.ArrayList;
import java.util.List;
@Controller
@RequestMapping("/person")
public class DataController {
private List<Person> people = new ArrayList<Person>();
@RequestMapping(value="put", method = RequestMethod.POST)
public void addPerson(@ModelAttribute("person") Person person){
System.out.println(">>>>>>> person: " + person);
System.out.println(">>>>>>>>> " + person.getFirstName());
people.add(person);
}
}
這裏是我的應用程序上下文XML文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
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="uk.co.jeeni" />
<mvc:annotation-driven />
</beans>
這裏是我的web.xml文件:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<web-app 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"
version="2.4">
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/data/*</url-pattern>
</servlet-mapping>
</web-app>
這裏是我的從我的HTML文件調用JQuery:
function attachSendDataEvent(){
$("#sendData").click(function(){
var data = "firstName=" + $("#firstName").val() + "&" +
"lastName=" + $("#lastName").val() + "&" +
"address=" + $("#address").val() + "&" +
"postcode=" + $("#postcode").val();
$.post("data/person/put",
data,
dataSentOK
);
});
return false;
}
dataSentOK
功能只是做一個alert("DONE")
。
所以當JQuery的方法調用的網址是:
http://localhost:8080/jquery/data/person/put
,並在服務器端的System.out.println(...)
方法打印出預期的數據。
但是在Firebug,服務器發回一個404
於是我打開與Spring記錄,並得到這樣的:
[01] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/put]
[02] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/put
[03] AbstractHandlerMethodMapping [DEBUG] Returning handler method [public void uk.co.jeeni.DataController.addPerson(uk.co.jeeni.Person)]
[04] AbstractBeanFactory [DEBUG] Returning cached instance of singleton bean 'dataController'
[05] DispatcherServlet [DEBUG] Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'person/put'; URL [person/put]] in DispatcherServlet with name 'dispatcherServlet'
[06] AbstractView [DEBUG] Added model object 'org.springframework.validation.BindingResult.person' of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name 'person/put'
[07] AbstractView [DEBUG] Added model object 'person' of type [uk.co.jeeni.Person] to request in view with name 'person/put'
[08] InternalResourceView [DEBUG] Forwarding to resource [person/put] in InternalResourceView 'person/put'
[09] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/person/put]
[10] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/person/put
[11] AbstractHandlerMethodMapping [DEBUG] Did not find handler method for [/person/person/put]
[12] DispatcherServlet [ WARN] No mapping found for HTTP request with URI [/jquery/data/person/person/put] in DispatcherServlet with name 'dispatcherServlet'
[13] FrameworkServlet [DEBUG] Successfully completed request
[14] FrameworkServlet [DEBUG] Successfully completed request
爲響應URL POST請求(/ jQuery的/數據/人/放)時,正確的方法是發現和調用(線1〜7),但然後在第8行,這會改變網址/jquery/data/person/person/put
向前春天到InternalResourceView
,這不能被發現。
如何阻止Spring嘗試查找視圖。我希望它做的就是乾乾淨淨地完成工作。
感謝您的幫助。
感謝您的回覆。答案是添加@ResponseBody。春天似乎處理無效返回罰款。 – 2013-02-26 10:25:15
@AdamDavies將其作爲答案發布並將其標記爲接受 – 2013-02-27 12:50:57