Thanks, overriding encodeBegin() works great! Is this solution considered a 'workaround' or is it perfectly expected way to use encodeBegin()? (What I did was add my initialization code to my overridden encodeBegin() then at the very end call super.encodeBegin().)
I found something you said interesting - could you give an example of what kinds of component initialization one might be doing immediately upon a client's postback (in decode())? (I can understand initialization when the component tree is generated on the server, because you'd want to manipulate things before displaying it - hence initializing in encodeBegin(), but I can't see cases of the other way around.) Thanks. Simon Kitching wrote: > > 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) > > -- View this message in context: http://www.nabble.com/Difficulties-of-initializing-a-custom-component-tp22655436p22667449.html Sent from the MyFaces - Users mailing list archive at Nabble.com.

