Not showing error when calling another function in Spring

Posted by Javi on Stack Overflow See other posts from Stack Overflow or by Javi
Published on 2010-03-23T09:33:18Z Indexed on 2010/03/23 9:43 UTC
Read the original article Hit count: 306

Filed under:
|
|
|

Hello,

I have a controller with 2 methods. The first one just add some lists to the model (the lists of my comboboxes) and return a JSP with a form. The second one validates the data and if there aren't errors it saves the data. The code looks like this:

public String showForm(Model model){
    //load some data into some lists
    return "tiles:myJSPfile";
}

public String save(@Valid @ModelAttribute("MyData") Data data, BindingResult result, Model model){
    if(result.hasErrors()){
        //there are errors: show form with error messages 
        //(but I need to reload the combobox lists)
        return showForm(model);
    }
    //save data
}

The problem is that in this way I don't see the error messages in the form (though I see by the debugger that it has found errors). I've thought that the problem comes because the showForm method doesn't have the error messages because it can't see the BindingResult, so I've added BindingResult as a parameter of showForm method, but it still doesn't work. I've added My Model Attribute as well but I get the same problem.

If I just return the JSP file in the save method I can see the errors, but in this way I would need to add again in this other method the lists I had already added in the showForm (duplicated code). I can't save the data in session scope so it's compulsory to get again the lists after getting the error.

    if(result.hasErrors()){
        //there are errors: show form with error messages
        return "tiles:myJSPfile";
    }

Why are errors not shown when I call a function to display my JSP different from the one I used for the validation? What's the best way to do this?

Thanks.

© Stack Overflow or respective owner

Related posts about spring-mvc

Related posts about validation