【问题标题】:Comparing entities with EL equality operator does not ever seem to return true将实体与 EL 相等运算符进行比较似乎永远不会返回 true
【发布时间】:2015-07-17 15:25:51
【问题描述】:

我正在遍历一个实体列表,呈现一些带有特定图标的链接。我想在迭代期间根据当前活动课程有条件地设置class属性值,如下所示,以便“活动”项目获得不同的样式:

<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>

在比较时它似乎永远不会返回true,因此永远不会打印green。我搜索了示例并使用#{view.viewId} 找到了some solutions,但这不符合我的要求。

我怎样才能达到我的要求?

【问题讨论】:

    标签: list jsf el equality


    【解决方案1】:

    如果Lesson 实体没有正确实现Object#equals() 方法,则此构造将失败,因此表示相同“值”的两个实例将永远不会相等。

    采取相应的行动。下面是一个基于实体技术 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);
    }
    

    另见:

    【讨论】:

    • 我试过这个'#{lessonBean.lsn.id == course.id? 'green' : ''}' 仍然没有实现
    • 那么那些不返回预期值?
    • 好吧,然后确保它们返回预期值,以便比较将返回true
    猜你喜欢
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多