在调用POST方法后重新调整Spring Controller 404 [英] Spring Controller 404 retuned after POST method Invoked

查看:98
本文介绍了在调用POST方法后重新调整Spring Controller 404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a Spring controller which is being called from JQuery.post(). When it is called the controller's method is invoked and returns. But then, in the background, Spring changes the URL and calls the server gain. The server responds with a 404.

我认为这是对Spring在处理POST方法后试图查找视图的回应.

I think this is in response to Spring trying to find a View after the POST method has been processed.

如何停止Spring控制器执行此操作.

How do I stop the Spring controller from doing this.

这是我的Spring Controller:

Here is my 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文件:

Here is my application context xml file:

<?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文件:

Here is my web.xml file:

<?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调用:

Here is my JQuery call from my HTML file:

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是:

So when the JQuery method the URL invoked is:

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

,在服务器端,System.out.println(...)方法可以按预期打印出数据.

and on the server side the System.out.println(...) method print out the data as expected.

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

However in Firebug, the server sends back a 404.

所以我打开了Spring的日志记录,并得到了:

So I switched on logging with Spring and got this:

[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,它会更改/jquery/data/person/person/put的URL,但找不到.

In response to the URL POST request (/jquery/data/person/put), the correct method is found and invoked (lines 1 to 7), but then Spring forwards to an InternalResourceView at line 8, which changes the URL to /jquery/data/person/person/put, and this can not be found.

如何阻止Spring尝试查找所有视图.我要它做的就是干净地做完.

How do I stop Spring from trying to find a view at all. All I want it to do is return cleanly and be done.

感谢您的帮助.

推荐答案

已解决.

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

The problem was as #CodeChimp suggested, except I still wanted a void return type.

我在addPerson方法中添加了@ResponseBody,并且一切正常:

I added the @ResponseBody to the addPerson method and everything worked fine:

@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 .尽管文档尚不清楚,否则返回空值会发生什么.只是尝试了一下,它就起作用了.

The clue came from http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-responsebody. Although the documentation is not clear what happens with a void return. Just tried it and it worked.

这篇关于在调用POST方法后重新调整Spring Controller 404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆