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

  • Displaying error messages.

    Hi,

    I have a simple login form which captures a uname/passwd for authentication. No Spring Security being used (yet). The problem is when the authentication fails, an exception is raised which is caught by the controller and I save a error message w/ a token as follows -

    Code:
    protected void saveErrors(HttpServletRequest request, String errorMsg) {
    List<String> errors = new ArrayList<String>();
    errors.add(errorMesg);
    request.getSession().setAttribute("errors", errors);
    }
    Now accessing this "errors" List in my JSP is kindda driving me nuts, cos' it seems to be empty if i do this-

    Code:
    <c:if test="${not empty errors}">
    //paint errors one by one
    </c:if>
    If I try to access (for testing purpose) the same as-

    Code:
    <%List<String> errors = (List<String>) request.getSession().getAttribute("errors");%>
    
    out.println(errors.get(0));
    ...it works absolutely fine.

    I'm not quiet sure what I'm missing here. I tried out the various "*Scope.errors" in case that was the problem, but in vain.

    Is there a simpler way to return error messages & paint them in Spring MVC?

    Any help is appreciated. Thanks in advance.

  • #2
    If you are using the spring form tags (<form:form>, <form:checkbox> ....), you may use <form:errors path="*" /> to display all error messages.

    Comment


    • #3
      I've always found it easier to display error messages using spring's validation framework rather than doing anything else. For your purpose, you could reject (using BindException.rejectValue()) the login name or password upon an unsuccessful attempt and then use hasBindErrors tag and/or errors tag.

      Comment


      • #4
        Thank you both for your suggestions.

        @Rober2D2
        Would you know if by using <form:errors/> tag one can manage the placement of the actual error text (along w/ some CSS) anywhere on the page, or does it by defautl come up next to each of the fields that have failed?

        @NubKnacker
        Yup, the validation code will definitely help w/ the error handling, but I would like to use a declarative validation framework (Commans maybe) instead of having to write code.

        Thanks once again.

        Comment


        • #5
          Originally posted by geekinfo View Post
          Thank you both for your suggestions.

          @Rober2D2
          Would you know if by using <form:errors/> tag one can manage the placement of the actual error text (along w/ some CSS) anywhere on the page, or does it by defautl come up next to each of the fields that have failed?

          Thanks once again.
          You may narrow the errors to be displayed, putting a field name in the "path" property. And the tag also has a cssClass attribute. Unfortunately, it is not much flexible regarding to CSS

          Comment

          Working...
          X