Announcement Announcement Module
Collapse
No announcement yet.
Error Handling in MultiActionController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error Handling in MultiActionController

    Is there a recommended way of error handling when using the MultiActionController. Since this class doesnt really expose the command object paradigm, I was wondering what approach might be used to create errors and redisplay back on the submitting page. I noticed that the BindException takes a command object in the constructor to represent the command object, which is not available. I would like to make use of the Spring taglibs in the JSP to display errors because of the ease of use.

    Does anyone have a recommendation for solving this issue??

  • #2
    KISS Approach

    Here's a simple way to do it. Just put the following saveError() method into your controller:

    Code:
        public void saveError(HttpServletRequest request, String msg) {
            List errors = (List) request.getAttribute("errors");
            if (errors == null) {
                errors = new ArrayList();
            }
            errors.add(msg);
            request.setAttribute("errors", errors);
        }
    And then add something like the following to your JSP.

    Code:
    <c&#58;if test="$&#123;not empty errors&#125;">
        <div class="error">
            <c&#58;forEach var="error" items="$&#123;errors&#125;">
                <img src="<c&#58;url value="/images/iconWarning.gif"/>"
                    alt="<fmt&#58;message key="icon.warning"/>" class="icon" />
                <c&#58;out value="$&#123;error&#125;" escapeXml="false"/><br />
            </c&#58;forEach>
        </div>
    </c&#58;if>

    Comment


    • #3
      Hi,

      The command object holds the form data and is processed in formControllers. In MultiActionController, we do not need to bind any data to the command object, thats why we use MultiActionController.

      If you want to add your hand-made error messages and view them from your jsps, you may put them to your model (by adding an ArrayList holding all your error messages to the model Map).

      --
      Lemi Orhan Ergin

      Comment


      • #4
        Solution

        mraible -

        Thanks for that code snippet. That definately got me going in the right direction, but I wanted to be able to handler things similarly in the Spring MVC whether I was using a Command-bound Controller (like SimpleFormController) or an unbound Controller (like Controller or MultiActionController).

        Here is what I have come up with so far:

        I created an implementation of org.springframework.validation.Errors called UnboundException (similar to BindException) that is bound to the HttpServletRequest instead of a Command object. I can now use JSPs the same way, whether in a command-bound or unbound scenario with the same <spring:bind> tag paradigm and the form fields are re-populated from the request if I re-display the same page (in case of error). I am not finished testing this paradigm yet, but the results so far are very promising...

        One of the things the Spring Web Framework stresses is flexibility and this is a great example. I suppose the next step could be the creation of an AbstractUnboundController that makes use of this exception, has a "successView" without having "commandName" or "commandClass" parameters...

        Comment

        Working...
        X