且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

每个请求都会重新创建复合组件中的支持bean

更新时间:2021-06-27 03:39:01

有一个主要的误解就在这里。那不是支持豆。这是一个支持组件。

There's a major misconception going here. That's not a backing bean. That's a backing component.

JSF UI组件实例不是视图范围,而是请求范围。它们在渲染响应结束时被破坏(在将状态保存到JSF视图状态之后)并在视图构建期间重新创建(并且它们的状态从JSF视图状态恢复)。

JSF UI component instances are not view scoped, instead they are request scoped. They are destroyed by end of render response (after having saved their state into JSF view state) and recreated during view build time (and their state is restored from JSF view state).

您已将有状态属性指定为组件的实例变量。这个不对。您应该明确地将它们存储在JSF状态中。正确的方法是让getter和setter委托给 UIComponent#getStateHelper() 。声明为< cc:attribute> 的任何属性都已隐式执行此操作。您绝对不需要将它们重新声明为支持组件的实例变量。

You've assigned the stateful properties as instance variables of the component. This is not right. You should be explicitly storing them in the JSF state. The correct approach for that is to let the getter and setter delegate to UIComponent#getStateHelper(). Any attributes which are declared as <cc:attribute> already implicitly do that. You do absolutely not need to redeclare them as instance variables of the backing component.

那些未声明为< cc:attribute> 的布尔值必须重新实现,如下所示:

Those booleans which are not declared as <cc:attribute> must be reimplemented like follows:

public Boolean getRenderUserId() {
    return (Boolean) getStateHelper().eval("renderUserId", Boolean.FALSE);
}

public void setRenderUserId(Boolean renderUserId) {
    getStateHelper().put("renderUserId", renderUserId);
}

在你的行动(听众)方法中,只需调用因此,setRenderUserId(true)

In your action(listener) method, just invoke setRenderUserId(true) accordingly.

不要忘记相应地修复EL表达式:

Don't forget to fix the EL expressions accordingly:

#{cc.renderUserId} 



参见: