You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Michal Prinke <bo...@icpnet.pl> on 2004/07/16 02:30:12 UTC
Lazy initialization in getter? Why @Inserts are read during rewind?
Hello,
I've put some @Inserts inside @Form. It appears that their values are
read during the rewind phase! I thought that @Inserts have nothing to do
with forms - they only output some HTML... unfortunately, this behaviour
causes me problems.
I have a text field named "X" and some lazy-initialized value named "Z".
The value of Z is calculated from the submitted X.
So, when the form is first processed by Tapestry, the value of Z is
read. Z is null, it must be initialized, so it reads the value of X in
order to perform some fancy stuff (e.g. fetching data from database).
Since Z is placed in HTML before X, the value of X is not yet known, and
the calculated value of Z is incorrect (bases on null).
How to solve this problem?
My only idea is to abandon lazy initialization of Z and explicitly set
it in form's listener method, when all the submitted parameters are
known. Is it the way it should be done?
The source of my page:
<html><body>
<form jwcid="@Form" listener="ognl:listeners.onSubmit">
Z (just a static text): <span jwcid="@Insert" value="ognl:Z"/>
X (text field): <input jwcid="@TextField" value="ognl:X"/>
<input jwcid="@Submit"/>
</form>
</body></html>
And here's the class source:
public abstract class SomePage extends BasePage {
public abstract String getX(); // property defined in .page file
private String z = null;
public String getZ() // used via OGNL
{
if (z == null) // lazy initialization
z = fetchFromDatabase(getX());
return z;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org