Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 弹簧控制器404在调用POST方法后重新调谐_Jquery_Spring Mvc - Fatal编程技术网

Jquery 弹簧控制器404在调用POST方法后重新调谐

Jquery 弹簧控制器404在调用POST方法后重新调谐,jquery,spring-mvc,Jquery,Spring Mvc,我有一个从JQuery.post()调用的Spring控制器。调用时,将调用控制器的方法并返回。但是,在后台,Spring更改URL并调用服务器增益。服务器以404响应 我认为这是对Spring在处理POST方法后试图找到一个视图的回应 如何阻止Spring控制器执行此操作 这是我的Spring控制器: import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.

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

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

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

这是我的Spring控制器:

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);
  }
}
dataSentOK
函数只执行一个
警报(“完成”)

因此,当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行),但随后会跳转到第8行的
InternalResourceView
,这会将URL更改为
/jquery/data/person/person/put
,但找不到该方法

我如何阻止Spring试图找到一个视图呢。我想要它做的就是干净利落地返回并完成


感谢您的帮助。

我相信如果您有一个null或void返回类型,Spring将尝试根据请求URL解析视图。我认为这里的正确形式应该是简单地返回OK页面,因为这看起来不是JSON或类似的东西。或者,用@ResponseBody标记它并返回一个空字符串。

问题正如#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);
}

线索来自于。尽管文档不清楚作废退货会发生什么。刚刚试过,效果很好。

谢谢您的回复。答案是添加@ResponseBody。Spring似乎可以很好地处理void返回,我猜void或null返回与空字符串的工作原理相同。Spring只会在响应体中放入任何内容。我发现Spring的人非常聪明,他们在解决方案中倾向于考虑所有可能的角度。
http://localhost:8080/jquery/data/person/put
[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
@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);
}