29

我通过剖析示例应用程序来自学 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  

错误信息所指的方法是:

@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";
}  

当我尝试在 Web 浏览器中加载 /catowners url 模式时触发了此错误消息。我已经查看了此页面此帖子,但解释似乎不清楚。

谁能告诉我如何解决这个错误,并解释它的含义?

编辑:
根据 Biju Kunjummen 的回复,我将语法更改为以下内容:

@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid Integer typeID, BindingResult result, Map<String, Object> model)  

我仍然收到相同的错误消息。有什么我不明白的吗?

第二次编辑:

根据 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";
 }

在告诉 eclipse 运行为...再次在服务器上运行后,我仍然收到相同的错误消息。

有什么我不明白的吗?

4

2 回答 2

23

Spring 使用调用的接口HandlerMethodArgumentResolver来解析处理程序方法中的参数并构造一个对象以作为参数传递。

如果它没有找到,它通过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.

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.

于 2013-09-05T21:32:03.443 回答
7

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.

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, ...)
于 2013-09-05T21:32:21.837 回答