The only reason to use binding to a backing bean's UIComponent
instance that I know of is the ability to manipulate that component programmatically within an action/actionlistener method, or ajax listener method, like in:
UIInput programmaticInput;//getter+setter
String value1, value2;//getter+setter
...
public void modifyInput() {
ELContext ctx = FacesContext.getCurrentInstance().getELContext();
ValueExpression ve = FacesContext.getCurrentInstance().getApplication().getExpressionFactory().createValueExpression(ctx, "#{bean.value2}", Object.class);
programmaticInput.setValueExpression("value", ve);
}
After the action method has been triggered the value of component <h:inputText value="#{bean.value1}" binding="#{bean.programmaticInput} ... />
will be bound to value2
instead of value1
.
I rarely use this type of binding, because facelets offer an XML-based view definition without the necessity to (regularly) mess with programmatic components.
Be sure to know that the abovementioned construct fails in Mojarra version older than 2.1.18, forcing view scoped beans to be recreated on every HTTP request. For more details refer to @ViewScoped fails in tag handlers.
More typically, you'd want to use binding to the view in which you can do cross-field validation:
<h:inputText binding="#{input}" ... />
<h:inputText validator="#{bean.validate}" ... >
<f:attribute name="input" value="#{input}" />
</h:inputText>
Here, the whole first input component will be available as an attribute of the second component and therefore its value will be available in the associated validator (method). Another example is to check which of the command components has been triggered in view:
<h:commandButton binding="#{button}" ... />
<h:inputText disabled="#{not empty param[button.clientId]}" ... />
Here, the input text component will be disabled only when the button was pressed.
For more information proceed to the follwing answers by BalusC: