0

What happens: When I put a globalError on the BindingResult (using reject) it don't appear in my view.

My Controller:

    @RequestMapping("register/manageCliente/newcliente.do")
    public String newClienteDo(@ModelAttribute("formCliente") @Valid  Cliente formCliente, BindingResult bResult, Model model) throws Exception {

        if(!bResult.hasErrors()) {
            try {
              //Do Something

            } catch (SQLException e) {
                logger.error("No newClienteDo :" + e); //it works, the error appear into my log4j.
                bResult.reject(e.getMessage());
            }
        }

        if (bResult.hasErrors() || bResult.hasGlobalErrors()) {
            model.addAttribute("msgError",bResult.getAllErrors().toString()); //it works, if I put the model in the view

            return "register/manageCliente/newcliente";
        }

        model.addAttribute("formCliente",formCliente);

        return "redirect:/register/manageCliente/listcliente";

    }

On my view I put the <form:errors> on this way:

<form:form action="newcliente.do" method="post" modelAttribute="formCliente" commandName="formCliente">
    <form:errors></form:errors> 
</form:form>

I tried <form:errors path="*"> and don't work too.

It don't show any errors, I'm using log4j with log4j.logger.org.springframework=DEBUG active. It don't show any errors too.

Thank you,

Edited.

It is doing about 4 requests to the view, ever cleaning my rejects. I tried the solution with Post-Redirect-Get and don't work too.

Edited.2

Since its was an emergencial problem I put the errors on a model.addAttribute and print the errors with EL in my view. But its not correct, so if someone has a better solutions I'll keep this post opened.

  • Remove the `Model` parameter. You don't need to add the `formCliente` as it is already part of the model due to the `@ModelAttribute`. The `BindingResult` also has a model, (check the `getModel()` method on that class). Now your empty model overrides the one from the `BindingResult`. – M. Deinum Mar 17 '15 at 20:22
  • @M.Deinum I'll need to add another things to the model, how can I do this without a model parameter? Per example: In the future I'll need to add some Lists to the model to iterate in my view. –  Mar 17 '15 at 20:37
  • possible duplicate of [Spring MVC: Validation, Post-Redirect-Get, Partial Updates, Optimistic Concurrency, Field Security](http://stackoverflow.com/questions/29039116/spring-mvc-validation-post-redirect-get-partial-updates-optimistic-concurren) – Neil McGuigan Mar 18 '15 at 03:51
  • To prepare your model it is advisable to use `@ModelAttribute` annotated methods for that instead of putting all that stuff in your request handling method. If you really need to add things just do `bResult.getModel()` to get the model. – M. Deinum Mar 18 '15 at 06:50
  • @M.Deinum How can I add a `List` per example to go to a View using `@ModelAttribute`? Do you have an example of this? The `bResult` don't have an `bResult.setModel` to update the things in the model I can just cast the Model with getModel. –  Mar 19 '15 at 13:06
  • 1
    It has a `getModel` which gives you a `Map` that is the model. `Model` is nothing more then a wrapper around this map. Also as stated in your current method you don't have anything being added to the model that isn't already there, you are basically duplicating the things Spring already does for you. And as mentioned if you need something else added just add a `@ModelAttribute` annotated method. – M. Deinum Mar 19 '15 at 13:14

0 Answers0