微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个 Spring控制器正在从 JQuery.post()调用.当它被调用时,控制器的方法调用并返回.但是,在后台,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<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函数只执行一个警报(“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将转发到第8行的InternalResourceView,该URL将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.虽然文档不清楚,空白返回会发生什么.只是试过它,它的工作.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐