Results 1 to 4 of 4

Thread: onSubmit() doesn't show errors (Spring MVC 2.0)

  1. #1
    Join Date
    Oct 2006
    Location
    San Francisco Bay Area
    Posts
    11

    Default onSubmit() doesn't show errors (Spring MVC 2.0)

    In Spring MVC 2.0-RC4, when I've passed the validation checks but encounter an error condition (a Db/logic error, not a field validation error) in onSubmit(), I set the errors/BindException object and return:

    Code:
    protected ModelAndView onSubmit(HttpServletRequest httpServletRequest,
                                    HttpServletResponse httpServletResponse,
                                    Object command,
                                    BindException errors) throws Exception {
    
    BizObject bizObject = (BizObject) command;
    ...
        if (someDbCheckFails) {
            ModelAndView mav = new ModelAndView(getFormView(), "bizObject", bizObject);
            errors.reject("db.error.condition");
            mav.addObject("errors", errors);
            return mav;
        }
    ...
    The form view is returned but there are no error messages printed on the page. I have this snippet on the .jsp to print error messages:

    Code:
      <spring:bind path="bizObject.*">
          <c:if test="${not empty status.errorMessages}">
              <div class="error">
                  <c:forEach var="error" items="${status.errorMessage}">
                      <c:out value="${error}" escapeXml="false"/><br/>
                  </c:forEach>
              </div>
          </c:if>
      </spring:bind>
    There is no exception or stack trace. Any idea why the errors are not printing? There are no specific examples of this in any of the four books I've read on Spring; they only cover the simplest field-related validation errors. (TIA!)
    Last edited by psconnolly; Oct 4th, 2006 at 10:36 PM.

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    try status.errorMessages this is what we use.

    Also setting the errors object as you do I don't know if that works. I believe the way you do it, the current model is being overwritten by your new object.

    You might try the following

    Code:
    BizObject bizObject = (BizObject) command;
    
        if (someDbCheckFails) {
            errors.reject("db.error.condition");
            Map model = errors.getModel();
            model.put("bizObject", bizObject);
            return new ModelAndView(getFormView(), model);
        }
    Last edited by Marten Deinum; Oct 5th, 2006 at 03:05 AM.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    Oct 2006
    Location
    San Francisco Bay Area
    Posts
    11

    Smile

    Thanks! That change works. (It would be nice if RJohnson et. al. would include examples of this type of error handling in their documentation! Life does not begin and end with validation errors. )

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,625

    Default

    I believe the do use this in one of their sample applications. Not sure which one, but I saw it somewhere. Will try to see if I can find the source. Might come in handy..
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •