Walking further down the understanding process of the jsf 2 view scope, I am running into problems again.
There are multiple instances of my composite component's bound object created and setting values does not seem to target the "right" one.
I have the same initial setup as in Auto-instantiate session-scoped bean from view-scoped bean
Now I created a composite component:
<composite:interface componentType="helloWidget">
</composite:interface>
<composite:implementation>
<h:outputText value="Visible state of this composite component: #{cc.visibleState}"/>
</composite:implementation>
and its java counterpart
@FacesComponent(value = "helloWidget")
public class HelloWidget extends UINamingContainer implements Serializable {
private static final long serialVersionUID = 2L;
private boolean visible;
public void show() {
System.out.println("Setting visible state to true " + this);
visible = true;
}
public void hide() {
System.out.println("Setting visible state to false " + this);
visible = false;
}
public String getVisibleState() {
System.out.println("Getting visible state of " + this + "(" + visible + ")");
return String.valueOf(visible);
}
}
I then upgraded my ViewBean.java
private HelloWidget helloWidget;
private boolean visible;
public String getVisibleState() {
return String.valueOf(visible);
}
public void actionShow(ActionEvent ae) {
visible = true;
helloWidget.show();
}
public void actionHide(ActionEvent ae) {
visible = false;
helloWidget.hide();
}
public HelloWidget getHelloWidget() {
return helloWidget;
}
public void setHelloWidget(HelloWidget helloWidget) {
this.helloWidget = helloWidget;
}
and my hello.xhtml:
<f:view>
<h:form>
<h:outputText value="View-scoped bean visible value: #{viewBean.visibleState}"/>
<br/>
<mycc:helloWidget binding="#{viewBean.helloWidget}"/>
<br/>
<h:commandButton value="Show" actionListener="#{viewBean.actionShow}"/>
<h:commandButton value="Hide" actionListener="#{viewBean.actionHide}"/>
</h:form>
</f:view>
When I now hit the show / hide buttons, the value of the "visible" property in the view scoped bean changes as expected. The "visible" value of the HelloWidget property changes too, but when the page is displayed, a different HelloWidget instance is displayed, which has visible set to (default) false.
What am I doing wrong here? Is there a problem with the way I bind the composite component?