Insights into Java Server Faces


Pass component references using simple bindings management

Filed under: JSF — Hanspeter @ 22:02

Sometimes binding of UIComponent comes in very handy. For example if you want to pass client ids into a composite component to alow the cc to use these ids with attributes. It would be very boring to specify the full client ids explicitly. This article will show how component references can be passed on in a simple generic way.

The basic idea is to use a simple request scoped managed bean to keep binding references (remember? Binding must only be used with request scoped beans!). For simple access this bean will implement the Map interface, or even simpler extend HashMap. The get(key) and put(key,component) methods are overridden to handle change of ViewRoot – whenever ViewRoot changed between access to get/put method, the map is cleared() (see implementation of the Bindings-bean).

The use of this bindings is very simple. Consider the example below:

  <my:compositeComp ...
      <b>reRender="#{bindings.selectedCountry.clientId}"</b> ... />
  <h:outputLabel value="The selected country:" />
  <h:outputText value="#{autoComplete.firstCountry}" id="selectedCountry"
      <b>binding="#{bindings.selectedCountry}"</b> />

What does the example show:

  1. the with id=”selectedCountry” id bound to the bindings bean as the map entry with key “selectedCountry”
  2. gets the full clientId of the component from the bindings bean (see reRender param).
  3. as you might imagine, since bindings bean proviedes references to UIComponent, you can also access other UIComponent attributes with EL expressions.

Looks very simple – and works.

For simple use in Java code the Bindings bean has a public static getInstance() method. Therefore you can acquire the reference to the bound component like this:

  UIComponent component = (UIComponent)Bindings.getInstance().get("selectedCountry");

Hope you like it.

By dueni.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: