Tôi có bộ điều khiển Spring đang được gọi từ JQuery.post(). Khi nó được gọi là phương thức của controller thì được gọi và trả về. Nhưng sau đó, trong nền, Spring thay đổi URL và gọi các máy chủ đạt được. Máy chủ phản hồi với 404.Bộ điều khiển lò xo 404 được retuned sau phương thức POST Đã gọi
Tôi nghĩ điều này là để phản hồi lại Mùa xuân đang cố gắng tìm Chế độ xem sau khi phương thức POST đã được xử lý.
Làm cách nào để ngăn bộ điều khiển lò xo thực hiện việc này.
Đây là điều khiển Mùa xuân của tôi:
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);
}
}
Đây là ứng dụng tập tin xml bối cảnh của tôi:
<?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>
Dưới đây là file web.xml của tôi:
<?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>
Đây là của tôi Cuộc gọi JQuery từ tệp HTML của tôi:
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;
}
Chức năng dataSentOK
chỉ cần thực hiện alert("DONE")
.
Vì vậy, khi các phương pháp JQuery URL gọi là:
http://localhost:8080/jquery/data/person/put
và ở phía máy chủ System.out.println(...)
phương pháp in ra các dữ liệu như mong đợi.
Tuy nhiên trong Firebug, máy chủ sẽ gửi lại một 404.
Vì vậy, tôi bật lên đăng nhập với mùa xuân và nhận điều này:
[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
Để đối phó với các yêu cầu URL POST (/ jquery/data/người/đặt), phương pháp đúng được tìm thấy và gọi (dòng 1 đến 7), nhưng sau đó Spring chuyển tiếp đến một InternalResourceView
ở dòng 8, thay đổi URL thành /jquery/data/person/person/put
và không thể tìm thấy phương thức này.
Làm cách nào để ngăn Spring tìm cách xem toàn bộ. Tất cả những gì tôi muốn làm là trả lại sạch sẽ và hoàn thành.
Cảm ơn sự giúp đỡ của bạn.
Cảm ơn bạn đã trả lời. Câu trả lời là thêm @ResponseBody. Mùa xuân dường như xử lý trả về void tốt. –
@AdamDavies đăng nó làm câu trả lời và đánh dấu nó là được chấp nhận –