Results 1 to 5 of 5

Thread: Displaying error messages.

  1. #1
    Join Date
    Dec 2008
    Posts
    11

    Default 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. #2
    Join Date
    Dec 2007
    Posts
    130

    Default

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

  3. #3
    Join Date
    Oct 2008
    Location
    Delhi, India
    Posts
    163

    Default

    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.

  4. #4
    Join Date
    Dec 2008
    Posts
    11

    Default

    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.

  5. #5
    Join Date
    Dec 2007
    Posts
    130

    Default

    Quote 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

Posting Permissions

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