在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]
答案 0 :(得分:2)
这不是@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}
答案 1 :(得分:1)
您应该使用@RequestParam
代替@PathVariable
@RequestMapping(value = "/addName.htm", method = RequestMethod.POST)
public void setAllocations(@RequestParam String name) {
System.out.println("inside Setting value.... ");
System.out.println(name);
}
来自docs: