jquery - Spring MVC 3.0 带注释的 Controller 无法与 JQuery $.post() 一起使用

标签 jquery spring model-view-controller post annotations

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

JQuery 帖子

$.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]

最佳答案

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

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

这就是区别:

  • 路径变量:http://yourhost/{name}/addName.html
  • 请求参数:http://yourhost/addName.html?name={name}

关于jquery - Spring MVC 3.0 带注释的 Controller 无法与 JQuery $.post() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8047272/

相关文章:

java - 我应该在哪里实现 mvc 架构(spring + hibernate)中的特定查询?

asp.net-mvc - 适用于 ASP.Net MVC 4 的验证码 MVC 及文档

php - 业务逻辑和模板之间如何交互?

java - Spring Security 如何在 Servlet 上工作

jquery - (jQuery) addClass 到整行

javascript - 如何停止iframe内的点击

jquery - 使用 ScrollView 组件后无法在文本字段中键入文本

spring - 如何在 Spring Boot 2.0 Web 应用程序中指定图像存储的外部位置?

php - 可以访问 PHP MVC 框架中的公共(public)类

jquery - Google Maps API v3、jQuery UI 选项卡、 map 未调整大小