gpt4 book ai didi

jquery - Spring Controller 404在POST方法调用后返回

转载 作者:行者123 更新时间:2023-12-03 22:17:35 25 4
gpt4 key购买 nike

我有一个从 JQuery.post() 调用的 Spring Controller 。当它被调用时, Controller 的方法被调用并返回。但随后,Spring 在后台更改 URL 并调用服务器增益。服务器响应 404。

我认为这是对 Spring 在处理 POST 方法后尝试查找 View 的响应。

如何阻止 Spring Controller 执行此操作。

这是我的 Spring Controller :

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 方法调用的 URL 为:

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/data/person/put),找到并调用正确的方法(第 1 到 7 行),但随后 Spring 转发到位于以下位置的 InternalResourceView第 8 行,将 URL 更改为 /jquery/data/person/person/put,但找不到。

如何阻止 Spring 尝试查找 View 。我想要它做的就是干净地返回并完成。

感谢您的帮助。

最佳答案

已解决。

问题正如 #CodeChimp 所建议的那样,但我仍然想要一个 void 返回类型。

我将 @ResponseBody 添加到 addPerson 方法中,一切正常:

@RequestMapping(value="put", method = RequestMethod.POST)
**@ResponseBody**
public void addPerson(@ModelAttribute("person") Person person){
System.out.println(">>>>>>> person: " + person);
System.out.println(">>>>>>>>> " + person.getFirstName());
people.add(person);
}

线索来自http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody 。尽管文档并不清楚无效返回会发生什么。刚刚尝试了一下,成功了。

关于jquery - Spring Controller 404在POST方法调用后返回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com