Spring MVC 3.1 exception : An Errors/BindingResult

2019-07-24 19:15发布

I upgraded to Spring 3.1.1.RELEASE, and now I'm getting an exception on the following method:

@RequestMapping(method = RequestMethod.POST, params = "_finish")
public ModelAndView doPostFinish(@PathVariable("runEnvironmentName") RunEnvironment pEnvironment, @ModelAttribute("command") JobSpecNewCommand pCommand, BindingResult pErrors) 
{
...
}

Throws the following exception:

java.lang.IllegalStateException: An Errors/BindingResult argument is expected 
to be immediately after the model attribute argument in the controller method 
signature: 

java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be immediately after the model attribute argument in the controller method signature: public org.springframework.web.servlet.ModelAndView de.wwag.infra.oocsd.projectAdmin.fe.natures.hudson.jobspecs.RunEnvJobSpecNewController.doPostFinish(de.wwag.infra.oocsd.projectAdmin.common.domain.RunEnvironment,de.wwag.infra.oocsd.projectAdmin.fe.natures.hudson.jobspecs.JobSpecNewCommand,org.springframework.validation.BindingResult)
    at org.springframework.web.method.annotation.ErrorsMethodArgumentResolver.resolveArgument(ErrorsMethodArgumentResolver.java:62) ...


As you can see the method signature is as expected. The BindingResult argument is declared after the model attribute.

In the same class the following method is declared:

@ModelAttribute("otherJobSpecs")
public List<JobReference> modelOtherJobSpecs(@PathVariable("runEnvironmentName") RunEnvironment pEnvironment, @ModelAttribute("command") JobSpecNewCommand pCommand) 
{
...
}

When I remove the method from the class, everything works as expected.

Any ideas?

2条回答
beautiful°
2楼-- · 2019-07-24 20:01

Reading the Spring bug SPR-9378, you must remove the model variable from the @ModelAttribute method:

@ModelAttribute
public void populateModel(Model model) { //this method haven't Libro Object
    model.addAttribute(Genero.values());
    model.addAttribute(EstadoLibro.values());
}

@RequestMapping(method=RequestMethod.POST)
public String grabarLibro(@Validated @ModelAttribute Libro libro, BindingResult result) {
   ...
}

It worked for me.

查看更多
等我变得足够好
3楼-- · 2019-07-24 20:07

It's a bug in Spring, I reported it: SPR-9378.

查看更多
登录 后发表回答