• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Value and binding

 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Christopher Sharp
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, thanks, that is sort of helpful, but perhaps you could kindly give me a few lines of sample code.
 
Tim Holloway
Saloon Keeper
Posts: 27807
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic