Abbas Gadhia
Abbas Gadhia

Reputation: 15100

JSF 2 - Dynamically added component's ids are not retained on postback

I have a facet within a component within which i add components dynamically using

    UIComponent c = new UICustomComponent();
    c.setId("someIdGeneratedDynamically");
    facet.getChildren().add(c);

But on postback, when i iterate over this list, i realize that the components are present but their ids are different from the ones i specified earlier. (more specifically, these ids are of the form "j_id9, j_id10" etc)

I debugged a bit of code in StateManagementStrategyImpl and realized that they are deliberately not storing the component ids while saving the view.

My question is, why doesn't JSF store the component id?

Upvotes: 0

Views: 548

Answers (1)

mrembisz
mrembisz

Reputation: 12880

To answer your question stated at the end: in RestoreView phase, JSF rebuilds requested view from the template file(s). It is assumed components always receive the same ids as long as the templates don't change. The state is saved using clientIds as keys. If you modified the clientId programmatically it would be impossible to restore the state properly: the recreated component would have the original id and its state would be stored under another (modified) id. That's why it doesn't "store" clientId, it's that thing that's expected to remain constant and allows matching the recreated component with its state from the previous request.

I believe this behavior applies only to components created from the templates. JSF has a dedicated mechanism to handle programmatically added components and I would expect this mechanism to deal with clientIds as expected.

Upvotes: 1

Related Questions