Java Errors/BindingResult 参数应在模型属性、@RequestBody 或 @RequestPart 参数之后立即声明
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18646121/
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
An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments
提问by CodeMed
I am teaching myself Spring by dissecting example applications and then adding code here and there to test theories that I develop during the dissection. I am getting the following error message when testing some code that I added to a Spring application:
我通过剖析示例应用程序来自学 Spring,然后在这里和那里添加代码来测试我在剖析过程中开发的理论。在测试我添加到 Spring 应用程序的一些代码时,我收到以下错误消息:
An Errors/BindingResult argument is expected to be declared immediately after the
model attribute, the @RequestBody or the @RequestPart arguments to which they apply
The method to which the error message refers is:
错误信息所指的方法是:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(Integer typeID, BindingResult result, Map<String, Object> model) {
// find owners of a specific type of pet
typeID = 1;//this is just a placeholder
Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
model.put("selections", results);
return "owners/catowners";
}
This error message was triggered when I tried to load the /catowners url pattern in the web browser. I have reviewed this pageand this posting, but the explanation does not seem clear.
当我尝试在 Web 浏览器中加载 /catowners url 模式时触发了此错误消息。我已经查看了这个页面和这个帖子,但解释似乎不清楚。
Can anyone show me how to fix this error, and also explain what it means?
谁能告诉我如何解决这个错误,并解释它的含义?
EDIT:
Based on Biju Kunjummen's response, I changed the syntax to the following:
编辑:
根据 Biju Kunjummen 的回应,我将语法更改为以下内容:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid Integer typeID, BindingResult result, Map<String, Object> model)
I am still getting the same error message. Is ther something I am not understanding?
我仍然收到相同的错误消息。有什么我不明白的吗?
SECOND EDIT:
第二次编辑:
Based on Sotirios's comment, I changed the code to the following:
根据 Sotirios 的评论,我将代码更改为以下内容:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(BindingResult result, Map<String, Object> model) {
// find owners of a specific type of pet
Integer typeID = 1;//this is just a placeholder
Collection<Owner> results = this.clinicService.findOwnerByPetType(typeID);
model.put("selections", results);
return "owners/catowners";
}
I am still getting the same error message after telling eclipse to run as...run on server again.
在告诉 eclipse 运行为...再次在服务器上运行后,我仍然收到相同的错误消息。
Is there something that I am not understanding?
有什么我不明白的吗?
采纳答案by Sotirios Delimanolis
Spring uses an interface called HandlerMethodArgumentResolver
to resolve the parameter in your handler methods and construct an object to pass as an argument.
Spring 使用一个被调用的接口HandlerMethodArgumentResolver
来解析处理程序方法中的参数并构造一个对象作为参数传递。
If it doesn't find one, it passes null
(I have to verify this).
如果没有找到,则通过null
(我必须验证这一点)。
The BindingResult
is a result object that holds errors that may have come up validating a @ModelAttribute
, @Valid
, @RequestBody
or @RequestPart
, so you can only use it with parameters that are annotated as such. There are HandlerMethodArgumentResolver
for each of those annotations.
该BindingResult
是保存可能已经拿出了一个验证错误,结果对象@ModelAttribute
,@Valid
,@RequestBody
或者@RequestPart
,这样你就可以只与这样的注释参数使用它。HandlerMethodArgumentResolver
这些注释中的每一个都有。
EDIT (response to comment)
编辑(对评论的回应)
Your example seems to show that the user should provide a pet type (as an integer). I would change the method to
您的示例似乎表明用户应该提供宠物类型(作为整数)。我会将方法更改为
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@RequestParam("type") Integer typeID, Map<String, Object> model)
And you would make your request (depending on your config) as
你会提出你的请求(取决于你的配置)
localhost:8080/yourcontext/catowners?type=1
Here too there is nothing to validate so you don't want or need a BindingResult
. It would fail if you tried to add it anyway.
这里也没有什么需要验证的,所以你不需要或不需要BindingResult
. 如果您无论如何都尝试添加它,它将失败。
回答by Biju Kunjummen
If you have a parameter of type BindingResult
it is essentially to hold any errors when binding http request parameters to a variable declared directly preceding the BindingResult method parameter.
如果您有一个类型的参数,BindingResult
那么在将 http 请求参数绑定到直接在 BindingResult 方法参数之前声明的变量时,它本质上是保留任何错误。
So all of these is acceptable:
所以所有这些都是可以接受的:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid MyType type, BindingResult result, ...)
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@ModelAttribute MyType type, BindingResult result, ...)
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@RequestBody @Valid MyType type, BindingResult result, ...)