Understanding JSF components class lifetime

半城伤御伤魂 提交于 2019-12-11 10:53:55

问题


What's the component's lifetime? Traversing the UIInput sources I've noticed that there's local field called value which is essentailly the value of the component. I've also noticed that after processing all convertion and validation we compare a new value with an old value of the component and if they're different fire valueChange event. Actually, here's the piece of code taking over the quing events:

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

But if the component were killed after any request, we would simply get ValueChangeEvent eny time we send a request. So, I presume the component's lifetime is the same as the bean's lifetime which the component bound to the property to. But I couldn't find any documental assurance...


回答1:


Component instances are request scoped. Only component properties which are delegated to UIComponent#getStateHelper() are view scoped. I.e. they are saved in the JSF view state. This covers among others the ValueHolder#getLocalValue() which the getValue() is delegating to.

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

Only on stateless views, i.e. pages with <f:view transient="true">, the behavior as you described "we would simply get ValueChangeEvent eny time we send a request" will indeed happen.

See also:

  • 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?


来源:https://stackoverflow.com/questions/31955955/understanding-jsf-components-class-lifetime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!