Composite component pitfalls: Empty attributes

With this post I am going to start a small series about JSF composite component pitfalls. While composite components really simplify my life as JSF developer in most cases they (still) have some minor quirks from time to time.

In my first post in this series I would like to discuss problems with composite component attributes whose value is null.

In some cases, JSF cannot determine the correct type of a composite component attribute if the user of the component passes a value that is null. Let’s first have a look at a composite component that is causing this kind of trouble:

  <cc:attribute name="value"/>
  <cc:editableValueHolder name="input"/>
  <h:inputText id="input" value="#{cc.attrs.value}"/>

As you can see there is nothing fancy. Just an attribute with the name value that is used in a h:inputText component in the implementation part.

The problem occurs when users of the component pass a value expression to the attribute that resolves to null. If this is the case in the following example, JSF (at least in version 2.0 and 2.1) is not able to determine the correct type of the property. As a result no converter will be added to the component.

<jl:inputTest value="#{testController.longValue}">
  <f:validator for="input"

Having no converter can result in a ClassCastException in different places.

What can be done? Well, there are several possible fixes or workarounds for this problem:

  • MyFaces fixed this bug in versions 2.0.10 and 2.1.4. So any versions starting from those should be save. Unfortunately, this seems to be an issue with all 2.0 and 2.1 versions of Mojarra.
  • Use an explicit converter if possible.
  • Do not use null as the empty value if possible.
  • Wait for JSF 2.2 (see update)

This misbehavior was initially filed as an issue in Apache MyFaces (see [1] for details). Later it turned out that it is a general problem in the algorithm JSF specifies for resolving the type of the attribute. Therefore it was also filed as a JSF spec issue (see [2] for details) and will be fixed with JSF 2.2.

The source code for the presented example can be found in the JSFlive Github repository cc-empty-attribute.

Update: My post JSF 2.2: Empty composite component attributes features an updated example for JSF 2.2.

[1]: MYFACES-2552


2 responses to “Composite component pitfalls: Empty attributes

  1. Pingback: Composite component pitfalls - Empty attributes - Blog - Irian

  2. Pingback: JSF 2.2: Empty composite component attributes | JSFlive: Michael Kurz's JSF Weblog

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