*
The moose likes JSF and the fly likes Value and binding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "Value and binding" Watch "Value and binding" New topic
Author

Value and binding

Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 152
One thing that has confused me a lot, which applies to earlier versions of JSF, is the difference between a value attribute and a binding attribute. I've spent quite a lot of time Googling around on this, and even found some information on this forum posted some years ago, but the explanation wasn't very clear and an external link to an example given is now broken.

Basically, what is the difference between say, <h:inputText value="#{user.name}"/> and <h:inputText binding="#{anotherUser.anotherName}"/>, and how would they be used differently with their respective beans, and likewise for other tags? Perhaps a very simple example illustrating the differences between these would be very helpful indeed.

So far I have made extensive use of value attributes such as <h:inputText value="#{user.name}"/>, but not binding.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

Binding is something that has been overused, either because possibly in really old documentation it was the only way to do it or because certain lazy IDEs use it in favor of simpler, cleaner coding.

The difference between the value attribute and the binding attribute is that the binding maps a UI component to a backing bean property, but a value attribute maps a simple value or set of values to a backing bean property.

When you bind a component, you do it in JSF-specific terms and become heavily dependent on JSF APIs and internals. The value mapping, on the other hand, is almost entirely POJO. It's simpler to code and can be tested without re-creating the entire JSF runtime environment or major portions thereof.

Binding does have its uses, primarily when you want to to major dynamic redefining of the View, but that's hopefully going to be a rare event. Technical considerations aside, views that are too fluid confuse users. They prefer things to be where they expect them to be.

Customer surveys are for companies who didn't pay proper attention to begin with.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 152
OK, many thanks, but is it possible for you kindly to give a simple example where binding is better than using value, or in fact where value can't be used at all? Alterntaively, pehaps a link to some examples of where binding is used.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

I cannot offhand think of a single case where I'd use binding instead of value. There are some things such as panelGrid that can be bound, but don't have values, however.

I only have one project that I can recall that does binding. It's a generic database table editor that builds a table display that has a variable number of columns, based on the number of columns in the table being displayed. This is supposed to be an open-source project if I can ever get around to completing it. It binds the table to the backing bean so that logic in the backing bean can dynamically construct column UIComponents underneath and map their values to the DataModel, since the number of columns varies. There are extension taglibs that could do much the same, but this app was intended to use only core JSF.
Christopher Sharp
Ranch Hand

Joined: Dec 12, 2007
Posts: 152
Well, thanks, that is sort of helpful, but perhaps you could kindly give me a few lines of sample code.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15964
    
  19

Unfortunately, all I have at the moment is pretty ugly and not very compact. I suggest maybe looking at Kit Mann's JSF In Action book.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Value and binding
 
Similar Threads
NullPointerException in getter method
Difference between value attribute and binding attribute
backing beans vs managed beans
Difference between binding and value tag
JSF validation messages ... how to customize ?