myfaces-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Simon Kitching <skitch...@apache.org>
Subject Re: Difficulties of initializing a custom component
Date Mon, 23 Mar 2009 08:52:01 GMT
lightbulb432 schrieb:
> Where's the correct place to initialize a JSF custom component (in this case,
> a subclass of HtmlForm)? Because much of my initializing involves
> manipulating the component's children components, I decided to override
> getChildren() and do my initialization there - BAD IDEA!
> 
> ...it turns out that getChildren() is called BEFORE restoreState(), so when
> you submit the form, restoreState isn't called and getChildren() fails
> because it's in an uninitialized state.
> 
> I was thinking about using initializers or constructors, but then I figured
> to avoid it because some initialization could depend on the component
> properties getting populated first. (And initializers/constructors would be
> called before those properties get populated, via their setters.)
> 
> It looks like I'm running out of options - what's the best practice on this?
> Thanks.

Do you want to initialize this component just once, or repeat the logic
once for each http request?

To initialize the component when first created, I would suggest that the
best place is just before rendering starts. So encodeBegin() seems like
a good idea here. Of course if you only want this done once then you
would need a boolean "initialized" flag on the component, and skip
initializing if this is set.

If you also want to re-initialize the component at the start of a
postback, then perhaps decode() is the best place; it seems nicely
symmetric with using encodeBegin as the other hook.

Using restoreState for initialize-on-postback is also possible, but
restoreState is not called if the SERIALIZE_STATE_IN_SESSION
configuration flag is false. It is true by default, and I would
recommend to always leave it as true, so this is not a big issue.
Nevertheless, using decode avoids this problem.


Regards,
Simon
-- 
-- Emails in "mixed" posting style will be ignored
-- (http://en.wikipedia.org/wiki/Posting_style)

Mime
View raw message