Results 1 to 5 of 5

Thread: How does status.errormessage know which field to accossiate an error with?

  1. #1
    Join Date
    Sep 2007
    Posts
    5

    Default How does status.errormessage know which field to accossiate an error with?

    I'm going through a tutorial about creating a simple login procedure with SimpleFormController and trying to understand how to use the jstl-core tags to display login-errors to the user.

    What I cannot understand is why the <core:out value="${status.errorMessage}"/> is only displaying the error relating to the preceding field in the form. I realize this has something to do with the field value specified in the errors.rejectValue method call, but I cannot comprehend how the <core:out value="${status.errorMessage}"/> is linked to the correct field in the jsp-code. To me it like pure magic...

    Any help on this would be greatly appreciated!

    The code from the tutorial:

    -------- From logon.jsp --------
    <form method="post">
    <table width="25%" border="1">
    <tr>
    <td align="center" bgcolor="lightblue">Log on</td>
    </tr>
    <tr>
    <td>
    <table border="0" width="100%">
    <tr>
    <td width="33%" align="right">Username:</td>
    <td width="66%" align="left"><spring:bind
    path="credentials.username">
    <input type="text" name="username"
    value="<core:out value="${status.value}"/>" />
    </spring:bind></td>

    </tr>
    <tr>
    <td colspan="2" align="center"><spring:hasBindErrors
    name="credentials">
    <font color="red"> <core:forEach
    items="${status.errorMessages}" var="err">
    <core:out value="${err}" />
    </core:forEach> </font>
    </spring:hasBindErrors></td>
    </tr>
    <tr>
    <td width="33%" align="right">Password:</td>
    <td width="66%" align="left"><spring:bind
    path="credentials.password">
    <input type="password" name="password" />
    </spring:bind></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><spring:hasBindErrors
    name="credentials">
    <font color="red"><core:out value="${status.errorMessage}" /></font>
    </spring:hasBindErrors></td>
    </tr>
    <tr>
    <td align="center" colspan="2"><input type="submit"
    alignment="center" value="Logon"></td>
    </tr>
    </table>

    </td>
    </tr>
    </table>

    </form>
    --------


    -------- From messages.properies -------
    error.login.not-specified=User credentials not specified (try guest/guest).
    error.login.invalid-user=Username not valid, try 'guest'
    error.login.invalid-pass=Password not valid, try 'guest'
    --------


    -------- From LogonValidator.java ------
    public void validate(Object obj, Errors errors) {
    Credentials credentials = (Credentials) obj;
    if (credentials == null) {
    errors.rejectValue("username", "error.login.not-specified", null,
    "Value required.");
    } else {
    logger.info("Validating user credentials for: "
    + credentials.getUsername());
    if (credentials.getUsername().equals("guest") == false) {
    errors.rejectValue("username", "error.login.invalid-user",
    null, "Incorrect Username.");
    } else {
    if (credentials.getPassword().equals("guest") == false) {
    errors.rejectValue("username", "error.login.invalid-pass",
    null, "Incorrect Password.");
    }
    }

    }
    }
    --------------

  2. #2
    Join Date
    Sep 2007
    Posts
    3

    Default

    when you validate a field like this:
    errors.rejectValue("username", "error.login.invalid-pass", null, "Incorrect Password.");
    You associate the error message with the field "username", and you can display it inside the spring tag that is bound to that same field.

  3. #3
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by Joakim View Post
    What I cannot understand is why the <core:out value="${status.errorMessage}"/> is only displaying the error relating to the preceding field in the form. I realize this has something to do with the field value specified in the errors.rejectValue method call, but I cannot comprehend how the <core:out value="${status.errorMessage}"/> is linked to the correct field in the jsp-code. To me it like pure magic...
    Are you sure this really works, not just by coincidence? Especially the first one following username input should give you all error messages of the form since you don't set the context to credentials.username.

    You should also consider using the form taglib instead of the low-level bind tags.

    Joerg
    This post can contain insufficient information.

  4. #4
    Join Date
    Sep 2007
    Posts
    5

    Default

    Are you sure this really works, not just by coincidence? Especially the first one following username input should give you all error messages of the form since you don't set the context to credentials.username.
    You are absolutely right, this does give me all the errors in the first tag - just as expected. I accidentally pasted the wrong code in my previous post. My appologies.

    However, I'm still confused. The original jsp-code was as follows:

    --------
    <form method="post">
    <table width="25%" border="1">
    <tr>
    <td align="center" bgcolor="lightblue">Log on</td>
    </tr>
    <tr>
    <td>
    <table border="0" width="100%">
    <tr>
    <td width="33%" align="right">Username: </td>
    <td width="66%" align="left">
    <spring:bind path="credentials.username">
    <input type="text" name="username" value="<core:out value="${status.value}"/>"/>
    </spring:bind>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
    <spring:hasBindErrors name="credentials">
    <font color="red"><core:out value="${status.errorMessage}"/></font>
    </spring:hasBindErrors>
    </td>
    </tr>
    <tr>
    <td width="33%" align="right">Password: </td>
    <td width="66%" align="left">
    <spring:bind path="credentials.password">
    <input type="password" name="password" />
    </spring:bind>
    </td>
    </tr>
    <tr>
    <td colspan="2" align="center">
    <spring:hasBindErrors name="credentials">
    <font color="red"><core:out value="${status.errorMessage}"/></font>
    </spring:hasBindErrors>
    </td>
    </tr>
    <tr>
    <td align="center" colspan="2">
    <input type="submit" alignment="center" value="Logon">
    </td>
    </tr>
    </table>
    </td>
    </tr>
    </table>
    </form>
    ----------

    The way I understand things, <spring:hasBindErrors name="credentials"> will display any errors relating to the commandObject credentials. However, when I run this code while submitting a correct username but an invalid password, the first spring:hasBindErrors-tag doesn't display anything. The error message is only displayed in the second tag (ie under the password field). I guess my puzzelment comes down to why it is not displayed the first time around? I cannot se any way the tag is "connected" to a specific field in the jsp-code.

    Thank you again for helping me out!

  5. #5
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by Joakim View Post
    I guess my puzzelment comes down to why it is not displayed the first time around? I cannot se any way the tag is "connected" to a specific field in the jsp-code.
    Me neither No idea if this is a bug or a left-over from old behavior when it probably keeps the last element that something was bound to. You'd need to debug to see what's going on if you are interested enough - or just change it from credentials to credentials.<yourFieldName>, e.g. credentials.username and credentials.password.

    Joerg
    This post can contain insufficient information.

Posting Permissions

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