JSF component binding - some confusion
Asked Answered
C

1

6

From web pages like this one,

http://www.jsftutorials.net/components/step5.html

I understand that the binding attribute in JSF tag/view component is to bind the view component to a Java instance of the UI component in the backing bean.

E.g., that's what is done in the following code:

<h:inputText value="#{ myBean.someProperty}" binding="#{ myBean.somePropertyInputText}"/>

But sometimes I see code like this:

<h:commandButton id="t1" binding="#{foo}" value="Hello, World!" onclick="alert('I am #{id:cid(foo)}'); return false;" />

where id:cid is a taglib function which is defined as follow:

public static String cid(UIComponent component) {
    FacesContext context = FacesContext.getCurrentInstance();
    return component.getClientId(context);
}

In the above code, binding="#{foo}" does not bind to "a Java instance of the UI component in the backing bean".

So what is the meaning of expressions such as binding="#{foo}" ?

Coastland answered 29/3, 2012 at 16:29 Comment(0)
G
10

It just binds the component to the current Facelet scope. This is particularly useful if you don't need it in the backing bean at all. This saves your backing bean code from useless properties which aren't been used in any of the other methods at all. Note that it also works that way in JSF 1.2. Not sure about JSF 1.0/1.1 though as it uses a different and JSF-proprietary EL API.

See also:

Glowing answered 29/3, 2012 at 19:51 Comment(1)
Thank you so much for this answer and your answer at the attached link! It was very helpful and informative.Coastland

© 2022 - 2024 — McMap. All rights reserved.