23

I have read about component binding with binding attribute in following questions:

I understand that it binds the UI component behind the JSF tag in the view to an UIComponent property in the backing bean. However, I am confused what the use of component binding is and when we should use it. Can someone explain it in a more simpler way and give some practical examples?

Community
  • 1
  • 1
benz
  • 4,561
  • 7
  • 37
  • 68

2 Answers2

40

You should use it if you want to have access to the entire UIComponent instead of just only its value. For example, to access some methods which can't be invoked/bound in the view. This is answered in the 1st question you found: JSF component binding - some confusion

The 2nd question which you found, component binding vs findComponent() - when to use which?, merely answers "binding versus findComponent()", it does not answer "binding versus value" at all as you seem to think. Please don't get confused by this. value would obviously win over binding.

In real world code, component binding to the backing bean is often only used whenever the developer needs to manipulate its children programmatically by e.g. component.getChildren().add(...). The bean should however be request scoped. A broader scope may lead to inconsitenties as component instances are basically created on a per-request basis and shouldn't be shared across multiple requests. The view scope can also, but this is very memory inefficient, and on Mojarra versions older than 2.1.18, partial state saving must also be turned off, otherwise the view scoped bean instance referenced by binding will implicitly be recreated on every request. See also JSTL in JSF2 Facelets... makes sense? for a related answer.

It's also possible to bind the component to "the view". E.g.

<h:someComponent binding="#{some}">

This refers to an instance of UIComponent in the Facelet scope ("page scope"). This enables you to use for example #{some.clientId}, #{some.value} elsewhere in the same page. Note that no backing bean is involved here. See also JSF component binding without bean property.

Here are some real world use appliances of binding attribute:

Kalle Richter
  • 8,008
  • 26
  • 77
  • 177
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • thanks a ton BalusC. I understood your point perfectly. I will read the links provided and get back to you if find any more queries. – benz Sep 20 '12 at 18:50
  • Thanks @BalusC For your explanation. Please is it possible to bind a UIComponent from a list of UIComponent. e.g List uicomponents... – Cocoa Butter Apr 21 '15 at 16:52
  • It seems we may also use `binding` when using `` as an alternative to ``. Probably not a good idea as BalusC describes, but there you have it. – DavidS Jun 09 '15 at 23:34
  • Actually, I'm not sure it can be entirely avoided, because what if the action listener needs to manipulate some backing bean properties? I guess we could get the bean in the `NormalActionListener` (above comment) using the `FacesContext`. – DavidS Jun 09 '15 at 23:42
  • @DavidS: `` (and `` and ``) aren't "component bindings" as in binding `UIComponent` instances. – BalusC Jun 10 '15 at 06:58
2

read this answer:

What is the advantages of using binding attribute in JSF?

However, a lot of people in the community do not recommend binding. See this article for example:

http://drewdev.blogspot.com/2009/01/jsf-component-binding-stinks.html

Community
  • 1
  • 1
fareed
  • 3,034
  • 6
  • 37
  • 65
  • 2
    The author of the blog post has used `binding` for every component after a suggestion from a mailing list and now expresses his frustration with this very bad idea. He thinks that therefore the concept of `binding` is bad and shouldn't be used which is not better than first wrong approach. He suggests that the designers should be killed. It's much better to understand what concepts are for and use them as intended. – Kalle Richter May 07 '18 at 11:34