You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by De...@ak.blm.gov on 2005/08/10 19:52:20 UTC

Saving state with custom UIComponents

Fellow MyFacelings, 

I have a custom tag (child of UIComponentTagBase) and a custom UIComponent 
(child of UIComponentBase) .  In setProperties of the tag handler, I'm 
creating a bunch of UIComponents (such as HtmlInputText) and I am adding 
them as children to the UIComponent .  After quite a bit of research, I am 
still having problems preserving the state of the controls.  Can anyone 
explain the interworking of saveState, restoreState, processSaveState, 
etc. ?  I am already aware of the order in which these are called. 

Dennis Byrne

Re: Saving state with custom UIComponents

Posted by Mike Kienenberger <mk...@gmail.com>.
I can't remember where I learned this methodology.  Might have been
from Kito's JSF in action book.   It does work, though.

On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
>  
> Not trying to start an argument on this one, I just have some questions. 
> Wouldn't getRenderResponse always return true at the point in the request
> processing cycle when UIComponents are created?  If so, wouldn't 
> addChildrenAndFacets always be called?  If so, wouldn't this mean that the
> values of the children were being recreated, rather than being restored? 
>  
> Dennis Byrne
>

Re: Saving state with custom UIComponents

Posted by De...@ak.blm.gov.
Not trying to start an argument on this one, I just have some questions. 
Wouldn't getRenderResponse always return true at the point in the request 
processing cycle when UIComponents are created?  If so, wouldn't  
addChildrenAndFacets always be called?  If so, wouldn't this mean that the 
values of the children were being recreated, rather than being restored?

Dennis Byrne

Re: Saving state with custom UIComponents

Posted by Mike Kienenberger <mk...@gmail.com>.
What both Craig and I are saying is that if you do nothing with state
saving for a component, by default you'll inherit state saving code
for your children.

If you define state saving code for your component, then you simply
call super.saveState()/restoreState() (like t:aliasBean and 
t:saveState) in order to inherit this behavior.

What you've described below should work to my limited knowledge.

I've done something similar in the past, and it's worked for me.

However I created the children in the Component's constructor:

    public RowAndColumnRelationshipsHtmlDataTable()
    {
        super();
        
        Application app = getFacesContext().getApplication();

        // If this is the first time we've shown the page, create the
children and facets.
        boolean isInitialRequest = getFacesContext().getRenderResponse();
        if (isInitialRequest)
        {
            addChildrenAndFacets();
        }
    }


-Mike


On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
>  
> Replies to both of you inline. 
>  
> > On 8/10/05, Mike Kienenberger <mk...@gmail.com> wrote:
>  > > Does your custom component have any state to save?  If not, I would
>  > > think that the default UIComponentBase behavior would handle saving
>  > > state for any facets and children without you needing to do anything
>  > > further.
>  
> I simplify my scenario - When the page is first requested, the tag handler
> is instantiated and setProperties is called. In setProperties I am giving
> the UIComponent an HtmlInputText child, passing 'foo' to
> HtmlInputText.setValue().  An input text box with 'foo' is rendered. 
> Because classic tags are recycled, the tag handler is not instantiated on
> subsequent page requests, setProperties is not called, and 'foo' is lost.  A
> blank input text box is rendered. 
>  
> Perhaps this is a better question: what, if anything, must my UIComponent (a
> direct decendant of UIComponentBase) do to insure the state of it's child is
> saved? 
> 
>  > As long as you either:
>  > 
>  > * Don't override the saveState() and restoreState() methods, or
>  > 
>  > * Call super.saveState() and super.restoreState() appropriately.
>  > 
>  
> Still missing something.  Looking at
> myfaces/custom/aliasbean/AliasBean.java, saveState and
> restoreState both call the super.saveState and super.restoreState .  I
> imagined component persistence to be the opposite - something that is called
> on the root of the UIComponent tree and cascaded down to every leaf.  If
> such a chain reaction were to start from the leaves, wouldn't those
> components higher up in the hierarchy be saved and restored more than once? 
>  
>  
> I haven't overidden saveState() or restoreState() ... this time. 
> I am not calling super.saveState() or super.restoreState() ... this time. 
> On past occastions, I have done both ;) 
> 
> 
>  > 
>  > > 
>  > > On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
>  > > >
>  > > > Fellow MyFacelings,
>  > > >
>  > > > I have a custom tag (child of UIComponentTagBase) and a custom
> UIComponent
>  > > > (child of UIComponentBase) .  In setProperties of the tag handler,
> I'm
>  > > > creating a bunch of UIComponents (such as HtmlInputText) and I am
> adding
>  > > > them as children to the UIComponent .  After quite a bit of research,
> I am
>  > > > still having problems preserving the state of the controls.  Can
> anyone
>  > > > explain the interworking of saveState, restoreState, 
>  > processSaveState, etc.
>  > > > ?  I am already aware of the order in which these are called.
>  > > >
>  > > > Dennis Byrne
>  > > >
>  > >
>

Re: Saving state with custom UIComponents

Posted by Sean Schofield <se...@gmail.com>.
Take a look at the source for tree2 or one of the other tomahawk
components.  You will get an idea of what is involved for a typical
component.

sean

On 8/10/05, Craig McClanahan <cr...@gmail.com> wrote:
> On 8/10/05, Mike Kienenberger <mk...@gmail.com> wrote:
> > Does your custom component have any state to save?  If not, I would
> > think that the default UIComponentBase behavior would handle saving
> > state for any facets and children without you needing to do anything
> > further.
> 
> As long as you either:
> 
> * Don't override the saveState() and restoreState() methods, or
> 
> * Call super.saveState() and super.restoreState() appropriately.
> 
> :-)
> 
> Craig
> 
> >
> > On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
> > >
> > > Fellow MyFacelings,
> > >
> > > I have a custom tag (child of UIComponentTagBase) and a custom UIComponent
> > > (child of UIComponentBase) .  In setProperties of the tag handler, I'm
> > > creating a bunch of UIComponents (such as HtmlInputText) and I am adding
> > > them as children to the UIComponent .  After quite a bit of research, I am
> > > still having problems preserving the state of the controls.  Can anyone
> > > explain the interworking of saveState, restoreState, processSaveState, etc.
> > > ?  I am already aware of the order in which these are called.
> > >
> > > Dennis Byrne
> > >
> >
>

Re: Saving state with custom UIComponents

Posted by De...@ak.blm.gov.
Replies to both of you inline.

> On 8/10/05, Mike Kienenberger <mk...@gmail.com> wrote:
> > Does your custom component have any state to save?  If not, I would
> > think that the default UIComponentBase behavior would handle saving
> > state for any facets and children without you needing to do anything
> > further.

I simplify my scenario - When the page is first requested, the tag handler 
is instantiated and setProperties is called. In setProperties I am giving 
the UIComponent an HtmlInputText child, passing 'foo' to 
HtmlInputText.setValue().  An input text box with 'foo' is rendered. 
Because classic tags are recycled, the tag handler is not instantiated on 
subsequent page requests, setProperties is not called, and 'foo' is lost. 
A blank input text box is rendered.

Perhaps this is a better question: what, if anything, must my UIComponent 
(a direct decendant of UIComponentBase) do to insure the state of it's 
child is saved?

> As long as you either:
> 
> * Don't override the saveState() and restoreState() methods, or
> 
> * Call super.saveState() and super.restoreState() appropriately.
> 

Still missing something.  Looking at 
myfaces/custom/aliasbean/AliasBean.java, saveState and restoreState both 
call the super.saveState and super.restoreState .  I imagined component 
persistence to be the opposite - something that is called on the root of 
the UIComponent tree and cascaded down to every leaf.  If such a chain 
reaction were to start from the leaves, wouldn't those components higher 
up in the hierarchy be saved and restored more than once? 

I haven't overidden saveState() or restoreState() ... this time.
I am not calling super.saveState() or super.restoreState() ... this time.
On past occastions, I have done both ;)

> 
> > 
> > On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
> > >
> > > Fellow MyFacelings,
> > >
> > > I have a custom tag (child of UIComponentTagBase) and a custom 
UIComponent
> > > (child of UIComponentBase) .  In setProperties of the tag handler, 
I'm
> > > creating a bunch of UIComponents (such as HtmlInputText) and I am 
adding
> > > them as children to the UIComponent .  After quite a bit of 
research, I am
> > > still having problems preserving the state of the controls.  Can 
anyone
> > > explain the interworking of saveState, restoreState, 
> processSaveState, etc.
> > > ?  I am already aware of the order in which these are called.
> > >
> > > Dennis Byrne
> > >
> >

Re: Saving state with custom UIComponents

Posted by Craig McClanahan <cr...@gmail.com>.
On 8/10/05, Mike Kienenberger <mk...@gmail.com> wrote:
> Does your custom component have any state to save?  If not, I would
> think that the default UIComponentBase behavior would handle saving
> state for any facets and children without you needing to do anything
> further.

As long as you either:

* Don't override the saveState() and restoreState() methods, or

* Call super.saveState() and super.restoreState() appropriately.

:-)

Craig

> 
> On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
> >
> > Fellow MyFacelings,
> >
> > I have a custom tag (child of UIComponentTagBase) and a custom UIComponent
> > (child of UIComponentBase) .  In setProperties of the tag handler, I'm
> > creating a bunch of UIComponents (such as HtmlInputText) and I am adding
> > them as children to the UIComponent .  After quite a bit of research, I am
> > still having problems preserving the state of the controls.  Can anyone
> > explain the interworking of saveState, restoreState, processSaveState, etc.
> > ?  I am already aware of the order in which these are called.
> >
> > Dennis Byrne
> >
>

Re: Saving state with custom UIComponents

Posted by Mike Kienenberger <mk...@gmail.com>.
Does your custom component have any state to save?  If not, I would
think that the default UIComponentBase behavior would handle saving
state for any facets and children without you needing to do anything
further.

On 8/10/05, Dennis_Byrne@ak.blm.gov <De...@ak.blm.gov> wrote:
>  
> Fellow MyFacelings, 
>  
> I have a custom tag (child of UIComponentTagBase) and a custom UIComponent
> (child of UIComponentBase) .  In setProperties of the tag handler, I'm
> creating a bunch of UIComponents (such as HtmlInputText) and I am adding
> them as children to the UIComponent .  After quite a bit of research, I am
> still having problems preserving the state of the controls.  Can anyone
> explain the interworking of saveState, restoreState, processSaveState, etc.
> ?  I am already aware of the order in which these are called.   
>  
> Dennis Byrne
>