jquery – Spring控件404在POST方法调用后重新调用

前端之家收集整理的这篇文章主要介绍了jquery – Spring控件404在POST方法调用后重新调用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个 Spring控制器正在从 JQuery.post()调用.当它被调用时,控制器的方法调用并返回.但是,在后台,Spring更改URL并调用服务器的增益.服务器响应404.

我认为这是响应Spring尝试在POST方法被处理后找到一个View.

如何阻止Spring控制器这样做?

这是我的Spring Controller:

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.ModelAttribute;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestBody;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8.  
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. @Controller
  13. @RequestMapping("/person")
  14. public class DataController {
  15.  
  16. private List<Person> people = new ArrayList<Person>();
  17.  
  18. @RequestMapping(value="put",method = RequestMethod.POST)
  19. public void addPerson(@R_404_13@("person") Person person){
  20. System.out.println(">>>>>>> person: " + person);
  21. System.out.println(">>>>>>>>> " + person.getFirstName());
  22. people.add(person);
  23. }
  24. }

这是我的应用程序上下文xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  8. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  9. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <context:component-scan base-package="uk.co.jeeni" />
  12.  
  13. <mvc:annotation-driven />
  14.  
  15. </beans>

这是我的web.xml文件

  1. <?xml version="1.0" encoding="ISO-8859-1" ?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  5. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
  6. version="2.4">
  7.  
  8. <servlet>
  9. <servlet-name>dispatcherServlet</servlet-name>
  10. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  11. <init-param>
  12. <param-name>contextConfigLocation</param-name>
  13. <param-value>classpath*:applicationContext-web.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17.  
  18. <servlet-mapping>
  19. <servlet-name>dispatcherServlet</servlet-name>
  20. <url-pattern>/data/*</url-pattern>
  21. </servlet-mapping>
  22. </web-app>

这是我的HTML文件中的JQuery调用

  1. function attachSendDataEvent(){
  2. $("#sendData").click(function(){
  3.  
  4. var data = "firstName=" + $("#firstName").val() + "&" +
  5. "lastName=" + $("#lastName").val() + "&" +
  6. "address=" + $("#address").val() + "&" +
  7. "postcode=" + $("#postcode").val();
  8.  
  9. $.post("data/person/put",data,dataSentOK
  10. );
  11. });
  12.  
  13. return false;
  14. }

dataSentOK函数只执行一个警报(“DONE”).

所以当JQuery方法调用的URL是:

  1. http://localhost:8080/jquery/data/person/put

而在服务器端,System.out.println(…)方法打印出预期的数据.

但是在Firebug中,服务器发回404.

所以我开始使用Spring进行日志记录,得到:

  1. [01] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/put]
  2. [02] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/put
  3. [03] AbstractHandlerMethodMapping [DEBUG] Returning handler method [public void uk.co.jeeni.DataController.addPerson(uk.co.jeeni.Person)]
  4. [04] Abstractbeanfactory [DEBUG] Returning cached instance of singleton bean 'dataController'
  5. [05] DispatcherServlet [DEBUG] Rendering view [org.springframework.web.servlet.view.InternalResourceView: name 'person/put'; URL [person/put]] in DispatcherServlet with name 'dispatcherServlet'
  6. [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'
  7. [07] AbstractView [DEBUG] Added model object 'person' of type [uk.co.jeeni.Person] to request in view with name 'person/put'
  8. [08] InternalResourceView [DEBUG] Forwarding to resource [person/put] in InternalResourceView 'person/put'
  9. [09] DispatcherServlet [DEBUG] DispatcherServlet with name 'dispatcherServlet' processing POST request for [/jquery/data/person/person/put]
  10. [10] AbstractHandlerMethodMapping [DEBUG] Looking up handler method for path /person/person/put
  11. [11] AbstractHandlerMethodMapping [DEBUG] Did not find handler method for [/person/person/put]
  12. [12] DispatcherServlet [ WARN] No mapping found for HTTP request with URI [/jquery/data/person/person/put] in DispatcherServlet with name 'dispatcherServlet'
  13. [13] FrameworkServlet [DEBUG] Successfully completed request
  14. [14] FrameworkServlet [DEBUG] Successfully completed request

响应URL POST请求(/ jquery / data / person / put),找到并调用正确的方法(第1行到第7行),但是Spring将转发到第8行的InternalResourceView,该URL将URL更改为/ jquery / data / person / person / put,这是无法找到的.

如何阻止Spring试图找到一个视图.所有我想要做的都是干净地返回并完成.

谢谢你的帮助.

解决方法

解决了.

问题是#CodeChimp建议,除了我仍然想要一个void返回类型.

添加了@ResponseBody到addPerson方法,一切正常:

  1. @RequestMapping(value="put",method = RequestMethod.POST)
  2. **@ResponseBody**
  3. public void addPerson(@R_404_13@("person") Person person){
  4. System.out.println(">>>>>>> person: " + person);
  5. System.out.println(">>>>>>>>> " + person.getFirstName());
  6. people.add(person);
  7. }

线索来自http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody.虽然文档不清楚,空白返回会发生什么.只是试过它,它的工作.

猜你在找的jQuery相关文章