Announcement Announcement Module
No announcement yet.
Displaying a global form error Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Displaying a global form error

    In my SimpleFormController derived class, in onSubmit(), when the form contents are invalid I need to return the form page with an error displayed.

    i.e. You have a form for user authentication that requires a username and password, in onSubmit() you lookup this information in your datastore layer, and when the login is invalid display a message indicating this.

    In the onSubmit() I thought I could do this -

        protected ModelAndView onSubmit(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Object o, BindException e) throws Exception {
                if (invalidLogin(httpServletRequest)) {
                    Map myModel = new HashMap();
                    myModel.put("global.error","Invalid username or password");
                    return new ModelAndView(getFormView(),myModel);
               } else {
                   // return success view
    and in the form page simply have a tag -

    <c:out value="${global.error}"/>

    But this doesn't work, the form is displayed, but the message is not.


  • #2
    First of all: when redirecting to the form view, you should always use BindException.getModel() and you the returning map as the model. You can add your errors there. Everythign in the map is merged into the request (request scope) and should be visible to any JSP tags.

    There is a better mechanism to bind global errors by the way. Just use e.reject(...), which represents global errors.

    You can retrieve them by binding to the command object without any property I think.

    If the command object is bound under the name 'command' (which it is by default) you should be able to display errors using <spring:bind path="command"> and then using the status object (see the tag library doco for more info: