<c:if><c:否则>不起作用,两个条件的计算结果都像 true



我正在尝试根据列表是否包含给定的项目 ID 在两个不同的命令按钮之间切换:

               <c:if test="#{roomServiceManagerBean.holdinghotelvilla.contains(hotel.hotelvillaId)}">
                    <p:commandButton ajax="true" id="commanddeactivate" update=":roomserviceForm:hoteltable,:roomserviceForm:msgs" actionListener="#{roomServiceManagerBean.deactivateServiceForHotel(hotel.hotelvillaId)}" icon="ui-icon-radio-off" type="submit" value="Remove Service">
                        <f:param name="roomserviceid" value="#{roomServiceManagerBean.roomServiceId}" />
                    </p:commandButton>
                </c:if>
                <c:otherwise>
                    <p:commandButton id="commandactivate" update=":roomserviceForm:hoteltable,:roomserviceForm:msgs" actionListener="#{roomServiceManagerBean.activateServiceForHotel(hotel.hotelvillaId)}" icon="ui-icon-radio-on" type="submit" value="Provide Service">
                        <f:param name="roomserviceid" value="#{roomServiceManagerBean.roomServiceId}" />
                    </p:commandButton>
                </c:otherwise>

但是,它会失败,并且两个按钮都出现。这是如何造成的,我该如何解决?

如果

#{hotel}在视图构建时不可用,则此构造中的<c:if>将失败,但仅在视图渲染期间可用(例如,因为它被定义为<p:dataTable var>(。<c:otherwise>在这里完全被取代了。它属于一个<c:choose><c:when>。从技术上讲,您应该使用<c:if>而不是在test中否定。或者,您应该用<c:when>替换第一个<c:if>并将整个东西包装在<c:choose>中。但是,如果#{hotel}在视图构建期间不可用,则仍然会失败。

只需改用 JSF 组件的 rendered 属性即可。

<p:commandButton ... rendered="#{roomServiceManagerBean.holdinghotelvilla.contains(hotel.hotelvillaId)}">
    ...
</p:commandButton>
<p:commandButton ... rendered="#{not roomServiceManagerBean.holdinghotelvilla.contains(hotel.hotelvillaId)}">
    ...
</p:commandButton>

另请参阅:

  • c:choose中的EL表达式,无法使其工作
  • JSTL in JSF2 Facelets...意义?

最新更新