You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Max Muermann <ma...@gmail.com> on 2005/11/10 00:27:18 UTC

HtmlForm saving state and id generation

Hi all,

I have come across a problem with automatically generated ids for
components. I am using MyFaces-current from SVN together with Facelets 1.0d.

What happens is that in HtmlFormRendererBase.encodeBegin(), the last action
is to save the state of the form, which includes the state of all child
components. However, if a child component has not previously made a call to
getClientId(), it's id will be persisted as null.
In some cases, the first call that a component or its renderer make to
getClientId() is in the encodeBegin() method. As this method is not called
on a form's children until after the form's encoodeBegin() method has
completed, those components will never persist an id.

This causes problems when restoring the view, as those components will again
autogenerate ids, but unfortunately the numbering of the ids restarts at 0.

So, if my component tree looks like this:

HtmlForm
|-GoodComponent
|-BadComponent

Where GoodComponent gets its id before encodeBegin and BadComponent gets it
in encodeBegin, after the initial view creation, my ids look like this:

HtmlForm: _id0
|-GoodComponent: _id1
|-BadComponent: _id2

What is saved in the state, however, is this:

HtmlForm: _id0
|-GoodComponent: _id1
|-BadComponent: null

When this view is restored and null ids are regenerated, this happens:

HtmlForm: _id0
|-GoodComponent: _id1
|-BadComponent: _id0

Which of course plays havoc with delivering events to the correct component.

I have changed the HtmlFormRendererBase class to save the state in the
encodeEnd method, which seems to fix the problem, however I am not sure as
to the wider ramifications this might have.

What is the reason for saving the state at the beginning of the form (as the
comment in the code helpfully suggests)?

Cheers,
Max