Spring MVC 3.0 带注释的控制器无法与 JQuery $.post() 一起使用

发布于 2024-12-14 06:14:32 字数 598 浏览 2 评论 0原文

在 Spring MVC 控制器中,我尝试访问 JQuery 简单帖子发送的名称。 我收到错误在 @RequestMapping* 中找不到 @PathVariable [name] 我哪里出错了?

JQuery Post

$.post("addName.htm",{ name: "John"});

Spring Controller

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@PathVariable String name) {

    System.out.println("inside Setting value.... ");
    System.out.println(name);
}

我收到错误

在@RequestMapping 中找不到@PathVariable [name]

In the Spring MVC controller i am trying to access the name sent by the JQuery simple post.
I am getting error Could not find @PathVariable [name] in @RequestMapping*
Where i am going wrong?

JQuery Post

$.post("addName.htm",{ name: "John"});

Spring Controller

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@PathVariable String name) {

    System.out.println("inside Setting value.... ");
    System.out.println(name);
}

I am getting error

Could not find @PathVariable [name] in @RequestMapping

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

乖乖兔^ω^ 2024-12-21 06:14:32

这不是 @PathVariable,您必须使用 @RequestParam 代替。试试这个:

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@RequestParam String name) {
    System.out.println(name);
}

这就是区别:

  • PathVariable: http://yourhost/{name}/addName.html
  • RequestParam: http://yourhost/addName.html?name={name}

That is not a @PathVariable, you have to use a @RequestParam instead. Try this:

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@RequestParam String name) {
    System.out.println(name);
}

This is the difference:

  • PathVariable: http://yourhost/{name}/addName.html
  • RequestParam: http://yourhost/addName.html?name={name}
魔法唧唧 2024-12-21 06:14:32

您应该使用 @RequestParam 而不是

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@RequestParam String name) {
  System.out.println("inside Setting value.... ");
  System.out.println(name);
}

docs

  1. @PathVariable 注释参数,用于访问 URI 模板变量。
  2. @RequestParam注解的参数,用于获取具体的Servlet请求参数。

You should use @RequestParam instead of @PathVariable

@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@RequestParam String name) {
  System.out.println("inside Setting value.... ");
  System.out.println(name);
}

From the docs:

  1. @PathVariable annotated parameters for access to URI template variables.
  2. @RequestParam annotated parameters for access to specific Servlet request parameters.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文