@RequestParam 的使用在 Spring 3 中引发错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7484659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of @RequestParam throws error in Spring 3
提问by Vivek
I am using the @RequestParam annotation to get the request parameters , and using the same to insert the values into the DB. I have setup the controller to re-direct to the same page which contains the text fields for the user to enter the values, which are being read using the @RequestParam annotation.
我正在使用 @RequestParam 注释来获取请求参数,并使用相同的注释将值插入到数据库中。我已将控制器设置为重定向到包含供用户输入值的文本字段的同一页面,这些值正在使用 @RequestParam 注释读取。
But after I enter the values into the text fields , and click submit , it throws this error
但是在我将值输入文本字段并单击提交后,它会引发此错误
Request processing failed; nested exception is java.lang.IllegalArgumentException: Name for argument type [java.lang.String] not available, and parameter name information not found in class file either.
请求处理失败;嵌套异常是 java.lang.IllegalArgumentException: Name for argument type [java.lang.String] 不可用,并且在类文件中也找不到参数名称信息。
I am a newbie to Spring 3 , and unable to understand the error. Can anybody please shed light on the same.
我是 Spring 3 的新手,无法理解错误。任何人都可以请说明相同的情况。
Thanks in advance, Vivek
提前致谢,维维克
回答by Babu Subburathinam
In order to inject the value of request parameter into your handler method parameter, either one of the following should be satisfied
为了将请求参数的值注入您的处理程序方法参数,应满足以下任一条件
The name of the request parameter must match the name of the method parameter. e.g. Following will inject the request parameter named "studentName" into the method parameter studentName
public String goToStep(@RequestParam String studentName)The request parameter name must be explicitly specified if it does not match the method parameter. The following will inject "nameOfStudent" request parameter into studentName:
public String goToStep(@RequestParam("nameOfStudent") String studentName)
请求参数的名称必须与方法参数的名称匹配。例如下面将把名为“studentName”的请求参数注入到方法参数 studentName 中
public String goToStep(@RequestParam String studentName)如果请求参数名称与方法参数不匹配,则必须明确指定请求参数名称。以下将向 studentName 中注入“nameOfStudent”请求参数:
public String goToStep(@RequestParam("nameOfStudent") String studentName)
Please post your handler method code if your issue continues to persist.
如果您的问题仍然存在,请发布您的处理程序方法代码。
回答by sinuhepop
I asked for the version you are using because I ran with a similar problem a few days ago. I was using Spring 3.1.0.M2 and the same exception appeared when I was using @PathVariable in proxied @Controller.
我询问您正在使用的版本,因为几天前我遇到了类似的问题。我使用的是 Spring 3.1.0.M2,当我在代理的 @Controller 中使用 @PathVariable 时出现了同样的异常。
It was caused by a resolved known bug. You just have to switch to 3.0.6 or try the nightly build 3.1.0.BUILD-SNAPSHOT. Of course, the latter option is not recommended for production environment.
它是由已解决的已知错误引起的。您只需切换到 3.0.6 或尝试夜间构建 3.1.0.BUILD-SNAPSHOT。当然,生产环境不推荐使用后一种方式。

