本文介绍了预期在模型属性,@RequestBody或@RequestPart参数之后立即声明Errors / BindingResult参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我是在自学Spring,通过解剖示例应用程序,然后添加代码来测试我在解剖过程中形成的理论。测试一些我添加到Spring应用程序的代码时,我收到以下错误消息: 预计会出现Errors / BindingResult参数要在模型属性,它们应用的@RequestBody或@RequestPart参数之后立即声明 错误消息引用的方法是: @RequestMapping(value =/ catowners,method = RequestMethod.GET) public String findOwnersOfPetType(Integer typeID,BindingResult result,Map< String,Object> model){ //找到特定类型宠物的所有者 typeID = 1; / /这只是一个占位符 Collection< Owner> results = this.clinicService.findOwnerByPetType(typeID); model.put(选择,结果); 返回所有者/拥有者; } 这个错误信息是在我尝试加载/ 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){ //找到特定类型的宠物的所有者 Integer typeID = 1; //这只是一个占位符 Collection< Owner> results = this.clinicService.findOwnerByPetType(typeID); model.put(选择,结果); 返回所有者/拥有者; } 在eclipse运行后,我仍然收到相同的错误信息。再次运行在服务器上。 有没有什么我不理解的? 使用名为 HandlerMethodArgumentResolver 的接口来解析处理程序方法中的参数,并构造一个作为参数传递的对象。 如果找不到,它会传递 null (我必须验证这一点)。 BindingResult 是一个结果对象,它包含可能验证 @ModelAttribute , @有效的, @RequestBody 或 @RequestPart ,所以你只能使用它的参数被注释为这样。对于每个注释,都有 HandlerMethodArgumentResolver 。 编辑(回应评论) 您的示例似乎表明用户应该提供宠物类型(作为整数)。我会将该方法更改为 @RequestMapping(value =/ catowners,method = RequestMethod.GET) public String findOwnersOfPetType(@RequestParam(type)Integer typeID,Map< String,Object> model) 你可以根据你的配置提出你的请求: $ b $ pre $ local $ 8080 yourcontext catowners type = 1 这里也没有什么可以验证的,所以你不需要或者不需要 BindingResult 。如果您尝试添加它,它会失败。 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: An Errors/BindingResult argument is expected to be declared immediately after themodel attribute, the @RequestBody or the @RequestPart arguments to which they applyThe 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 page and this posting, but the explanation does not seem clear.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: @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: @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.Is there something that I am not understanding? 解决方案 Spring uses an interface called HandlerMethodArgumentResolver to resolve the parameter in your handler methods and construct an object to pass as an argument.If it doesn't find one, it passes null (I have to verify this).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=1Here 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. 这篇关于预期在模型属性,@RequestBody或@RequestPart参数之后立即声明Errors / BindingResult参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 08-22 13:59