简体   繁体   中英

How can i get error message in form:errors tag by using MultiActionController?

I have a form:

<form:form commandName="command" method="post">
    <form:errors path="property"/>
    // some fields
</form:form>

And a MultiActionController

Command command = new Command();
ServletRequestDataBinder binder = createBinder(request, command);
binder.bind(request);

for (Validator validator: getValidators())
    if(validator.supports(command.getClass()))
        validator.validate(command, binder.getBindingResult());

if(binder.getBindingResult().hasErrors())
    // What to put here in order to show error message in <form:errors path="property"/>

I have used:

return new ModelAndView()
                     .addObject(binder.getBindingResult().getModel()) 
                     .addObject("command", command);

But it does not work. When the form is shown again (after a failure) the command properties are shown but error messages are not.

ANSWER

instead of

return new ModelAndView()
                     .addObject(binder.getBindingResult().getModel()) 
                     .addObject("command", command);

I have to call

return new ModelAndView()
                     .addAllObjects(binder.getBindingResult().getModel());

Notice addAllObjects . Now it works fine!

Umm... it's too late at this point.

binder.getBindingResult() is an instance of BindingResult which is a subclass of Errors . Errors produced by both your bind() method invocation and your validators have already been added to that BindingResult instance.

If some of them happened to be associated with property path you'll see them in output of <form:errors> , otherwise you won't. You could, of course, iterate through them and re-add them back with the path in question but what would be the point?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM