在 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/