Announcement Announcement Module
Collapse
No announcement yet.
Displaying a global form error Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 -

    Code:
        protected ModelAndView onSubmit(HttpServletRequest httpServletRequest,
                                        HttpServletResponse httpServletResponse,
                                        Object o, BindException e) throws Exception {
    
    
                if (invalidLogin(httpServletRequest)) {
                    Map myModel = new HashMap();
                    myModel.putAll(e.getModel());
    
                    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.

    Andy.

  • #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: http://static.springframework.org/sp...lib/index.html).

    rgds,

    alef

    Comment

    Working...
    X