进一步了解 jsf 2 视图范围的过程,我再次遇到了问题。
我的复合组件的绑定对象创建了多个实例,并且设置值似乎没有针对“正确”的对象。
我的初始设置与中相同从视图范围的 bean 自动实例化会话范围的 bean https://stackoverflow.com/questions/15527304/auto-instantiate-session-scoped-bean-from-view-scoped-bean
现在我创建了一个复合组件:
<composite:interface componentType="helloWidget">
</composite:interface>
<composite:implementation>
<h:outputText value="Visible state of this composite component: #{cc.visibleState}"/>
</composite:implementation>
及其 java 对应项
@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);
}
}
然后我升级了我的 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;
}
和我的 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>
现在,当我点击显示/隐藏按钮时,视图作用域 bean 中“visible”属性的值将按预期更改。 HelloWidget 属性的“visible”值也会发生变化,但是当显示页面时,会显示不同的 HelloWidget 实例,该实例的visible 设置为(默认)false。
我在这里做错了什么?我绑定复合组件的方式有问题吗?