|
From: Christophe V. <c.v...@pa...> - 2003-09-17 18:55:32
|
Hello,
Is there an official way to have more than one form on the same page? Or to have a form (eg. a loginbox) on a "normal" index page?
The problem when you try to do something like that is that when you use the <spring:bind> tag, you'll get an exception if you haven't gone through your formcontroller (because there are no errors bound, and you can't create the BindStatus).
I solved the problem by putting a null check for the errors in BindTag, wich allows the page to be rendered even if you haven't gone through the controller (yet).
This method has two disadvantages:
- you no langer get an exception if you try to bind something which doesn't exist (in case of a typo, ...), but that was exactly what I wanted to be able to do :)
- if you have a form on the page, and you haven't gone through the controller yet, you're not able to prefill any values (because that is what you normally accomplish with getFormBackingObject() ).
The latter was solved by extending the tag and defining an extra attribute, altValue, which will be used for the BindStatus value if no errors where found (errors == null) in the requestContext (which is only possible if you haven't gone through the controller yet).
By extending, you have different tags, and you can keep the exceptions in forms where you don't need the extra functionality.
This way, one can have multiple forms on the same page, which will be handled by different controllers, and can be prefilled should one choose to.
From ExtBindTag.doStartTagInternal():
// retrieve errors object
this.errors = getRequestContext().getErrors(name, false);
if(this.errors == null) {
BindStatus status = new BindStatus(this.property,this.altValue, null,null);
this.pageContext.setAttribute(STATUS_VARIABLE_NAME,status);
return EVAL_BODY_INCLUDE;
}
Now I am able to do something like this on the main index page (written in JSP 2):
<form action="${url}" method="post">
<table>
//fill in the nick, with the value found in the cookie
//once we submitted, the "normal" status.value will be used, and not the
//value of the cookie
<spring:extBind path="userCredentials.nick" altValue="${cookie.nick.value}">
<tr><td>Nickname:</td></tr>
<tr><td><input type="text" name="nick" value="${status.value}"/></td></tr>
<tr><td>${status.errorMessage}</td></tr>
</spring:extBind>
//we don't need to prefill the password, use normal tag
<spring:bind path="userCredentials.password">
<tr><td>Password:</td></tr>
<tr><td><input type="password" name="password"/></td></tr>
<tr><td>${status.errorMessage}</td></tr>
</spring:bind>
<tr><td><input type="submit" value="login"/></td></tr>
</table>
</form>
If there was another (better :) way to solve this problem, please let me know.
Otherwise, this is probably a usefull extension.
--
Kind regards,
Christophe Vanfleteren
|