Java Errors/BindingResult参数应在model属性、@RequestBody或@RequestPart参数之后立即声明
我通过剖析示例应用程序来自学Spring,然后在这里和那里添加代码来测试我在剖析过程中开发的理论。在测试添加到Spring应用程序的某些代码时,我收到以下错误消息:Java Errors/BindingResult参数应在model属性、@RequestBody或@RequestPart参数之后立即声明,java,spring,hibernate,spring-mvc,Java,Spring,Hibernate,Spring Mvc,我通过剖析示例应用程序来自学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
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";
}
@RequestMapping(value=“/catowner”,method=RequestMethod.GET)
公共字符串FindDownersOfPtype(整数类型ID、BindingResult、映射模型){
//查找特定类型宠物的主人
typeID=1;//这只是一个占位符
收集结果=this.clinicService.FinDownerTypeType(typeID);
模型。放置(“选择”,结果);
返回“所有者/猫所有者”;
}
当我试图在web浏览器中加载/catowner url模式时,触发了此错误消息。我已经复习了,但解释似乎不清楚
有人能告诉我如何修复这个错误,并解释它的含义吗
编辑:根据Biju Kunjummen的回答,我将语法更改为:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid Integer typeID, BindingResult result, Map<String, Object> model)
@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";
}
@RequestMapping(value=“/catowner”,method=RequestMethod.GET)
公共字符串findOwnersOfPetType(@Valid-Integer-typeID,BindingResult,Map-model)
我仍然收到相同的错误消息。有什么我不明白的吗
第二次编辑:
根据Sotirios的评论,我将代码更改为:
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@Valid Integer typeID, BindingResult result, Map<String, Object> model)
@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";
}
@RequestMapping(value=“/catowner”,method=RequestMethod.GET)
公共字符串FindDownersOfpetype(BindingResult,映射模型){
//查找特定类型宠物的主人
整型typeID=1;//这只是一个占位符
收集结果=this.clinicService.FinDownerTypeType(typeID);
模型。放置(“选择”,结果);
返回“所有者/猫所有者”;
}
在告诉eclipse以…在服务器上再次运行之后,我仍然收到相同的错误消息
有什么我不明白的吗?Spring使用一个名为
HandlerMethodArgumentResolver
的接口来解析处理程序方法中的参数,并构造一个作为参数传递的对象
如果找不到,则传递null
(我必须对此进行验证)
BindingResult
是一个结果对象,它包含验证@ModelAttribute
、@Valid
、@RequestBody
或@RequestPart
时可能出现的错误,因此您只能将其与注释为此类的参数一起使用。每个注释都有HandlerMethodArgumentResolver
编辑(回复评论)
您的示例似乎表明用户应该提供一个宠物类型(作为整数)。我会将方法更改为
@RequestMapping(value = "/catowners", method = RequestMethod.GET)
public String findOwnersOfPetType(@RequestParam("type") Integer typeID, Map<String, Object> model)
这里也没有要验证的内容,因此您不希望或不需要
BindingResult
。如果您仍然尝试添加它,它将失败。如果您有一个类型为BindingResult
的参数,那么在将http请求参数绑定到BindingResult方法参数前面直接声明的变量时,基本上会保留任何错误
因此,所有这些都是可以接受的:
@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, ...)
哈哈,不要编辑我的问题。
HandlerMethodArgumentResolver
有更多规则。您希望Integer
对象来自哪里?@modeldattribute
是从HttpServletRequest
对象构建的,其他对象也是如此。我试图进行编辑,后来我刚刚对我自己的原始帖子进行了编辑。堆栈溢出不允许我删除对您帖子的意外编辑。你能推荐一些实际的代码来解决这个问题吗?这些答案似乎很深奥,我在上面的编辑中输入的实际代码并没有删除错误。@CodeMed整数从哪里来?您是否希望从url路径、某些请求参数等中获取此方法?此方法位于Spring PetClinic应用程序的OwnerController类中。我添加了这个方法作为一些更改的一部分,以创建一个返回给定类型宠物的所有主人的页面。随后,我希望不必为每种宠物类型创建单独的方法。然而,对于初学者来说,我只是试图在输出表中输出一个包含猫主人的页面。这就是抛出错误的方法。整数是petclinic应用程序数据库中CAT的类型_id=1。@据我所知,您希望用户提供一个类型。我的编辑展示了这一点。谢谢你花时间写了一个深思熟虑的答案。我用我认为你的建议对我上面的原始帖子进行了编辑,但仍然抛出了一条错误消息。有什么我不明白的吗?@Valid
带整数字段将不起作用。尝试使用包含类型,其中包含一个整数字段,后跟BindingResult
参数+1,谢谢。现在,我已使用此页面上的各种注释修复了该问题。