1

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...

St.Antario
  • 26,175
  • 41
  • 130
  • 318

1 Answers1

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Is it true that the ViewState decoding and setting the `defaultMap` property of the `ComponentStateHelper` (`StateHelper` for the `UIInput`) are performed during the ViewBuild time? So, if the value of a component is in ViewState, then we have the `isLocalValueSet()` returns `true` and the method getValue() just returns the value from the map? – St.Antario Aug 12 '15 at 08:11
  • No, it's performed during restore view phase. See also a.o. http://stackoverflow.com/q/31890433 to learn the difference. As to the `isLocalValueSet()`, this is just another property (also view scoped!) indicating that the local value is set, because it's valid to have a null local value and thus a nullcheck wouldn't work, the component would otherwise erroneously return model value via `getValue()`. – BalusC Aug 12 '15 at 08:14
  • Got it, thank you. I presume that's not directly related to the question, but how does JSF engine do things on the ViewState? Since you said __[...]are view scoped. I.e. they are saved in the JSF view state__, I'd guess that we have an empty view state to be created __iff__ `ViewId`'s changed. Is that correct? – St.Antario Aug 12 '15 at 08:28
  • The key is the value of the `javax.faces.ViewState` request parameter. – BalusC Aug 12 '15 at 08:48