3

I am trying to toggle between two different command buttons depending on whether a list contains the given item 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>

However, it fails and the both buttons appear. How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mark Seah
  • 532
  • 5
  • 18

1 Answers1

1

The <c:if> will in this construct fail if #{hotel} is not available during view build time but only during view render time (e.g. because it's definied as <p:dataTable var>). The <c:otherwise> is completely displaced here. It belongs to a <c:choose><c:when>. Technically, you should be using <c:if> instead with a negation in the test. Or, you should be replacing the first <c:if> by a <c:when> and wrap the whole thing in <c:choose>. However that would still fail if #{hotel} is not available during view build time.

Just use JSF component's rendered attribute instead.

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

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the prompt answer. I actually tried the rendered attribute before I switch to if else tag and it worked. however, one of the button actionListener does not fire. Any things I should look out for? – Mark Seah Sep 25 '13 at 03:23
  • See points 4 and 5 of http://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked – BalusC Sep 25 '13 at 03:41
  • Thank you so much, I followed your recommendation to have change the scope to viewscope. – Mark Seah Sep 25 '13 at 04:06