title: How to save state of custom attributes in backing component of a composite component?
body: <p>I've created a simple composite component with a backing component class. I'd like to save the state of a certain attributes of this class. Therefore I use the <code>StateHelper</code> within setter/getter of the attribute. From my perspective, if the component is derived from <code>UINamingContainer</code> this is all that needs to be done to let JSF save and restore state.</p>
  
  <p>Unfortunately this does not work as expected. There is a debug statement, that should print the state of the component to console whenever accessed. I expect to have state restored when the page is reentered (through navigation), but the state is always resolved to null.</p>
  
  <p>Example code:</p>
  
  <pre><code>@FacesComponent("myComponent")
  public class MyComponent extends UINamingContainer {
  
  enum PropertyKeys {selected}
  
  public void selectionListener(final AjaxBehaviorEvent event) {
               ...
      // do some stuff here
  
  }
  
  public void setSelected(final Collection&lt;Integer&gt; selected) {
  
      getStateHelper().put(PropertyKeys.selected, selected);
  
  }
  
  public Collection&lt;Integer&gt; getSelected() {
      Collection&lt;Integer&gt; state = (Collection&lt;Integer&gt;) getStateHelper().eval(PropertyKeys.selected);
  
      System.out.println("State: "+state);
  
      return state;
  }
  </code></pre>
  
  <p>What else needs to be done to keep state of a composite component saved during requests? In this case I'd like to provide a composite component that encapsulates multiple tables with row-selection features. I want the selected rows to be saved during requests within the component, not bound to a managed bean.</p>
  
  <p>In this special case, there will be a lot of selectable tables in the composite component and I want the composite component itself to keep track of selections so composite component users don't need to deal with multiple selection attributes.</p>
  
  <hr>
  
  <p><strong>Update:</strong> I created a simple example:</p>
  
  <p>Backing class:</p>
  
  <pre><code>@FacesComponent("StateSavinComponent")
  public class StateSavingComponent extends UINamingContainer{
  
      private String someText;
  
      private Object[] state;
  
      @Override
      public Object saveState(final FacesContext context) {
          if (state == null) {
              state = new Object[2];
          }
  
          state[0] = super.saveState(context);
          state[1] = someText;
          return state;
      }
  
      @Override
      public void restoreState(final FacesContext context, final Object state) {
          this.state = (Object[]) state;
          super.restoreState(context, this.state[0]);
          someText =  (String) this.state[1];
      }
  
      public void setSomeText(final String someText) {
          this.someText = someText;
      }
  
      public String getSomeText() {
          return someText;
      }
  
  }
  </code></pre>
  
  <p>Composite component:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:h="http://java.sun.com/jsf/html" 
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:cc="http://java.sun.com/jsf/composite"&gt;
  
      &lt;cc:interface componentType="StateSavinComponent"&gt;
  
      &lt;/cc:interface&gt;   
  
      &lt;cc:implementation&gt;
  
          &lt;h:inputText value="#{cc.someText}" /&gt;
  
      &lt;/cc:implementation&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>First page:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:h="http://java.sun.com/jsf/html" 
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:example="http://java.sun.com/jsf/composite/example"&gt;
  
  &lt;h:head&gt;
  
  &lt;/h:head&gt;
  &lt;h:body&gt;
  
      &lt;h:form id="form"&gt;
  
          &lt;example:stateSaving/&gt;      
  
          &lt;h:commandButton action="page2.xhtml" value="next" /&gt;
  
      &lt;/h:form&gt;
  
  &lt;/h:body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>Second page:</p>
  
  <pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" 
        xmlns:ui="http://java.sun.com/jsf/facelets" 
        xmlns:h="http://java.sun.com/jsf/html" 
        xmlns:f="http://java.sun.com/jsf/core"&gt;
  
  &lt;h:head&gt;
  
  &lt;/h:head&gt;
  &lt;h:body&gt;
  
      &lt;h:form id="form"&gt;
  
              &lt;h:commandButton action="example.xhtml" value="back" /&gt;
  
      &lt;/h:form&gt;
  
  &lt;/h:body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <ol>
  <li>Input value in text field</li>
  <li>navigate to page2.xhtml</li>
  <li>navigate back to example.xhtml</li>
  <li>The input field is empty, but should contain the value entered previously</li>
  <li>Setting breakpoints in the backing class</li>
  <li>saveState and restoreState seem to me to be called in the wrong order  </li>
  </ol>