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.");
}
}
}
}
--------------


Reply With Quote
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.