了解 JSF 组件 class 生命周期

Understanding JSF components class lifetime

组件的生命周期是多少?遍历 UIInput 来源,我注意到有一个名为 value 的本地字段,它本质上是组件的值。我还注意到,在处理完所有转换和验证之后,我们将组件的新值与旧值进行比较,如果它们不同,则触发 valueChange 事件。实际上,这是接管 quing 事件的代码片段:

if (isValid()) {
     Object previous = getValue();
     setValue(newValue);
     setSubmittedValue(null);
     if (compareValues(previous, newValue)) {
         queueEvent(new ValueChangeEvent(this, previous, newValue)); // <-----
     }
}

但是如果组件在任何请求后被杀死,我们每次发送请求时都会得到 ValueChangeEvent。因此,我假设组件的生命周期与组件绑定到 属性 的 bean 的生命周期相同。但是我找不到任何文件保证...

组件实例是请求范围的。只有委托给 UIComponent#getStateHelper() are view scoped. I.e. they are saved in the JSF view state. This covers among others the ValueHolder#getLocalValue() 的组件属性,getValue() 委托给

@Override
public Object getValue() {
    return isLocalValueSet() ? getLocalValue() : super.getValue();
}

仅在无状态视图上,即具有 <f:view transient="true"> 的页面,您描述的行为 “我们将在每次发送请求时简单地获取 ValueChangeEvent” 确实会发生.

另请参阅:

  • How does the 'binding' attribute work in JSF? When and how should it be used?
  • How to save state when extending UIComponentBase
  • What is the usefulness of statelessness in JSF?