在POST方法调用后,Spring Controller 404重新调整

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

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

如何阻止Spring控制器执行此操作。

这是我的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 people = new ArrayList(); @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文件:

      

这是我的web.xml文件:

    dispatcherServlet org.springframework.web.servlet.DispatcherServlet  contextConfigLocation classpath*:applicationContext-web.xml  1   dispatcherServlet /data/*   

这是我的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")

因此,当调用的URL的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 / data / person / put),找到并调用正确的方法(第1行到第7行),然后Spring转发到第8行的InternalResourceView ,这会将URL更改为/jquery/data/person/person/put ,这是找不到的。

如何阻止Spring尝试查找视图。 我想要它做的就是干净利落地完成。

谢谢你的帮助。

解决了。

问题是#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 。 虽然文档不清楚void返回会发生什么。 刚试了一下就行了。

我相信如果你有一个null或void返回类型,Spring将尝试根据请求URL解析视图。 我认为这里适当的forms是简单地返回一个OK页面,因为这似乎不是JSON或类似的东西。 或者,只需使用@ResponseBody标记它并返回一个空字符串。