【问题标题】:<c:if><c:otherwise> does not work, both conditions evaluate like true<c:if><c:otherwise> 不起作用,两个条件都评估为 true
【发布时间】:2013-09-24 21:14:07
【问题描述】:

我正在尝试根据列表是否包含给定的项目 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>

但是,它失败了,两个按钮都出现了。这是如何引起的,我该如何解决?

【问题讨论】:

    标签: jsf-2 primefaces jstl


    【解决方案1】:

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

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

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

    另见:

    【讨论】:

    • 感谢您的及时答复。在切换到 if else 标记之前,我实际上尝试了渲染属性并且它起作用了。但是,其中一个按钮 actionListener 不会触发。有什么需要注意的吗?
    • 非常感谢,我按照您的建议将范围更改为 viewscope。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-05-17
    • 1970-01-01
    • 2017-05-04
    • 1970-01-01
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多