Problem with ui:repeat and NULL values in JSF 2.x

Some months ago I stumbled across some issue with the ui:repeat component in JavaServer Faces 2. The project I worked on used JSF RI 2.0.2, but the problem seems to exist in all 2.x RI versions (especially the current versions 2.0.10 & 2.1.17).

The issue occurs with input fields (that implement EditableValueHolder) nested within ui:repeat. Once set a value it is impossible to reset the input components’ values to null (yes, even if not required and with javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL set to true).

I couldn’t believe such a problem has not been discovered and hence blamed myself. But while searching for solutions (or at least others with a similar problem) I found the following JIRA issue: Components inside ui:repeat can not be reset to null

The facelets component ui:repeat contains a static inner class named SavedState which is used for state saving during child iterations: Objects of this inner class are created and populated with their respective input field’s state as every component inside ui:repeat exists only once (within the component tree) and represents multiple values (one per iteration). The object’s method populate() reads the component’s current value via EditableValueHolder.getValue() during e.g. validation phase, which (in case of the local value being null) itself reads the value from the model . So the input field is restored with the old model value instead of being set to null.

Sadly the JSF RI team did not accept the above mentioned bug report (existing since 2010) and recently closed the issue against all comments, votings for the bug and explanations of the problem. Even worse: The bug seems already to exist in Facelets implementations used with JSF 1.2. According to the JSF team the bug is related to a specification issue described here – but I do not agree: In my opinion the bug is specific to ui:repeat and fixing the specification issue will not solve this bug.

As being unclear if the bug will be re-opened and fixed, I present a simple bugfix from the comments here (credits to bitec):

The component ui:repeat has to be re-defined in your project’s faces-config.xml, using the original RI source code as template which will be changed slightly:

<faces-config version="2.0" ..>
   <component>
      <component-type>facelets.ui.Repeat</component-type>
      <component-class>de.oio.project.facelets.component.UIRepeat</component-class>
   </component>
</faces-config>

Only one line within SavedState.populate(EditableValueHolder) has to be changed within your own UIRepeat class:

public void populate(EditableValueHolder evh) {
    this.value = evh.getLocalValue();
    this.valid = evh.isValid();
    this.submittedValue = evh.getSubmittedValue();
    this.localValueSet = evh.isLocalValueSet();
}
Short URL for this post: http://wp.me/p4nxik-1ii
This entry was posted in Java EE, Java Web Frameworks and tagged , , , . Bookmark the permalink.

6 Responses to Problem with ui:repeat and NULL values in JSF 2.x

  1. Banifou says:

    I just implemented and tested your solution. It works! I noticed this bug just yesterday. That is really strange why the JSF Team hasn’t fixed it yet – ONE liner!
    Thank u for this simple solution.

  2. Daniel Zwink Daniel Zwink says:

    Thanks for your comment!

    In the meanwhile a copy of the original issue has been created and the bug has been fixed. The bugfix applies since 2.1.21 and 2.2.0-m11 (see https://java.net/jira/browse/JAVASERVERFACES-2717).

  3. Arnis Rips says:

    Thank you.
    Thanks to your blog I was able to finally get this annoying bug fixed. I looked into it ~year ago and back then couldn’t find any valid fix and was able to bypass it. Sadly it resurfaced today causing serious trouble. :)

  4. Thomas Zuberbühler says:

    A colleague of mine did solve this issue in an application with JSF 1.2 and Richfaces 3.x by using .

  5. Luiz says:

    Thanks Daniel Zwink for the info about the bugfix!

Leave a Reply