|
|
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 lt;core ut value=quot;${status.errorMessage}quot;/gt; 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 lt;core ut value=quot;${status.errorMessage}quot;/gt; 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 --------
lt;form method=quot;postquot;gt;
lt;table width=quot;25%quot; border=quot;1quot;gt;
lt;trgt;
lt;td align=quot;centerquot; bgcolor=quot;lightbluequot;gt;Log onlt;/tdgt;
lt;/trgt;
lt;trgt;
lt;tdgt;
lt;table border=quot;0quot; width=quot;100%quot;gt;
lt;trgt;
lt;td width=quot;33%quot; align=quot;rightquot;gt;Username:lt;/tdgt;
lt;td width=quot;66%quot; align=quot;leftquot;gt;lt;spring:bind
path=quot;credentials.usernamequot;gt;
lt;input type=quot;textquot; name=quot;usernamequot;
value=quot;lt;core ut value=quot;${status.value}quot;/gt;quot; /gt;
lt;/spring:bindgt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td colspan=quot;2quot; align=quot;centerquot;gt;lt;spring:hasBindErrors
name=quot;credentialsquot;gt;
lt;font color=quot;redquot;gt; lt;core:forEach
items=quot;${status.errorMessages}quot; var=quot;errquot;gt;
lt;core ut value=quot;${err}quot; /gt;
lt;/core:forEachgt; lt;/fontgt;
lt;/spring:hasBindErrorsgt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td width=quot;33%quot; align=quot;rightquot;gt assword:lt;/tdgt;
lt;td width=quot;66%quot; align=quot;leftquot;gt;lt;spring:bind
path=quot;credentials.passwordquot;gt;
lt;input type=quot;passwordquot; name=quot;passwordquot; /gt;
lt;/spring:bindgt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td colspan=quot;2quot; align=quot;centerquot;gt;lt;spring:hasBindErrors
name=quot;credentialsquot;gt;
lt;font color=quot;redquot;gt;lt;core ut value=quot;${status.errorMessage}quot; /gt;lt;/fontgt;
lt;/spring:hasBindErrorsgt;lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td align=quot;centerquot; colspan=quot;2quot;gt;lt;input type=quot;submitquot;
alignment=quot;centerquot; value=quot;Logonquot;gt;lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/formgt;
---------------- 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(quot;usernamequot;, quot;error.login.not-specifiedquot;, null, quot;Value required.quot;); } else {logger.info(quot;Validating user credentials for: quot; + credentials.getUsername());if (credentials.getUsername().equals(quot;guestquot;) == false) { errors.rejectValue(quot;usernamequot;, quot;error.login.invalid-userquot;, null, quot;Incorrect Username.quot;);} else { if (credentials.getPassword().equals(quot;guestquot;) == false) { errors.rejectValue(quot;usernamequot;, quot;error.login.invalid-passquot;, null, quot;Incorrect Password.quot;); }}
} }
--------------
when you validate a field like this:
errors.rejectValue(quot;usernamequot;, quot;error.login.invalid-passquot;, null, quot;Incorrect Password.quot;);
You associate the error message with the field quot;usernamequot;, and you can display it inside the spring tag that is bound to that same field.
Originally Posted by JoakimWhat I cannot understand is why the lt;core ut value=quot;${status.errorMessage}quot;/gt; 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 lt;core ut value=quot;${status.errorMessage}quot;/gt; 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
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:
--------
lt;form method=quot;postquot;gt;
lt;table width=quot;25%quot; border=quot;1quot;gt;
lt;trgt;
lt;td align=quot;centerquot; bgcolor=quot;lightbluequot;gt;Log onlt;/tdgt;
lt;/trgt;
lt;trgt;
lt;tdgt;
lt;table border=quot;0quot; width=quot;100%quot;gt;
lt;trgt;
lt;td width=quot;33%quot; align=quot;rightquot;gt;Username: lt;/tdgt;
lt;td width=quot;66%quot; align=quot;leftquot;gt;
lt;spring:bind path=quot;credentials.usernamequot;gt;
lt;input type=quot;textquot; name=quot;usernamequot; value=quot;lt;core ut value=quot;${status.value}quot;/gt;quot;/gt;
lt;/spring:bindgt;
lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td colspan=quot;2quot; align=quot;centerquot;gt;
lt;spring:hasBindErrors name=quot;credentialsquot;gt;
lt;font color=quot;redquot;gt;lt;core ut value=quot;${status.errorMessage}quot;/gt;lt;/fontgt;
lt;/spring:hasBindErrorsgt;
lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td width=quot;33%quot; align=quot;rightquot;gt assword: lt;/tdgt;
lt;td width=quot;66%quot; align=quot;leftquot;gt;
lt;spring:bind path=quot;credentials.passwordquot;gt;
lt;input type=quot;passwordquot; name=quot;passwordquot; /gt;
lt;/spring:bindgt;
lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td colspan=quot;2quot; align=quot;centerquot;gt;
lt;spring:hasBindErrors name=quot;credentialsquot;gt;
lt;font color=quot;redquot;gt;lt;core ut value=quot;${status.errorMessage}quot;/gt;lt;/fontgt;
lt;/spring:hasBindErrorsgt;
lt;/tdgt;
lt;/trgt;
lt;trgt;
lt;td align=quot;centerquot; colspan=quot;2quot;gt;
lt;input type=quot;submitquot; alignment=quot;centerquot; value=quot;Logonquot;gt;
lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/tdgt;
lt;/trgt;
lt;/tablegt;
lt;/formgt;
----------
The way I understand things, lt;spring:hasBindErrors name=quot;credentialsquot;gt; 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 quot;connectedquot; to a specific field in the jsp-code.
Thank you again for helping me out!
Originally Posted by JoakimI guess my puzzelment comes down to why it is not displayed the first time around? I cannot se any way the tag is quot;connectedquot; 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.lt;yourFieldNamegt;, e.g. credentials.username and credentials.password.
Joerg |
|