webtier@glassfish.java.net

JSF2 programatically created components lost on postback?

From: <webtier_at_javadesktop.org>
Date: Wed, 01 Jul 2009 03:51:59 PDT

If I add components to an existing panelGroup in a facelet, these components will be rendered correctly on the client. Now, if I modify a value in one of these programmatically added components, the changes will not be applied after postback when using the default JSF2 viewhandler. However, switching to the legacy facelets 1.1.14 viewhandler, changes are applied.

It appears as the restored view only contains the facelets view without the programmatically added components. The problem can be seen in the following example. Is this a bug or am I doing something wrong?

[code]
<h:form id="form">
  <h:panelGroup id="viewPanel">
      <!-- dynamic created components are added here. -->
  </h:panelGroup>
  <h:commandButton id="render" value="render" action="#{render.render}"/>
</h:form>
[/code]

[code]
public class RenderBean {
        
        private String value;
        
        public RenderBean() {
                value = "foo";
        }
        
        public String getValue() {
                return value;
        }

        public void setValue(String value) {
                this.value = value;
        }
        
        public void render() {
                
                // <h:form id="form">
                // <h:panelGroup id="viewPanel"/>
                // </h:form>
                
                UIComponent viewPanel = FacesContext
                        .getCurrentInstance()
                        .getViewRoot()
                        .findComponent("form:viewPanel");

                viewPanel.getChildren().clear();

                UIComponent childPanel = new HtmlPanelGroup();
                childPanel.setId("childPanel");
                viewPanel.getChildren().add(childPanel);

                // Add a textinput to the inner most panel with a
                // binding to the the value property of this bean.
                // ...
                // <h:panelGroup id="viewPanel">
                // <h:panelGroup id="childPanel"/>
                // <h:textInput value="#{render.value}"/>
                // </h:panelGroup>
                // </h:panelGroup>
                // ...
                
                UIComponent textInput = new HtmlInputText();
                textInput.setId("textInput");
                textInput.setValueExpression("value", FacesContext.getCurrentInstance()
                                .getApplication().getExpressionFactory().createValueExpression(
                                                FacesContext.getCurrentInstance().getELContext(),
                                                "#{render.value}", Object.class));
                
                childPanel.getChildren().add(textInput);
        }
}
[/code]
[Message sent by forum member 'janderssn' (janderssn)]

http://forums.java.net/jive/thread.jspa?messageID=353741