0

I'm iterating over a list of entities, rendering some links with specific icons. I'd like to conditionally set a class attribute value during the iteration based on the currently active lesson as below, so that the "active" item gets a different style:

<ui:repeat value="#{lessonBean.allLessons}" var="lesson">
    <li>
        <h:form>
            <h:commandLink>
                <i class="#{lessonBean.currentLesson == lesson ? 'green' : ''}" />
            </h:commandLink>
        </h:form>
    </li>                                        
</ui:repeat>

It only doesn't seem to ever return true on the comparison and thus green is never printed. I've searched for examples and found some solutions using #{view.viewId}, but this doesn't suit my requirement.

How can I achieve my requirement?

Community
  • 1
  • 1
Anan Raddad
  • 195
  • 2
  • 7

1 Answers1

0

This construct will fail if the Lesson entity doesn't have the Object#equals() method properly implemented and thus two instances representing the same "value" will never be equal.

Act accordingly. Here's an example based on entity's technical ID:

private Long id;

@Override
public boolean equals(Object other) {
    return (other != null && getClass() == other.getClass() && id != null)
        ? id.equals(getClass().cast(other).id)
        : (other == this);
}

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555