开发者

Two <h:commandButton> in a <h:form>, one work and one doesn't

I'm trying to use one <h:commandButton> with ajax to check some conditions, if it's OK the other <h:commandButton> with no ajax will be display. The one with ajax work well, but the other doesn't although I have specified a return String to another page. I cannot figure out why it failed, the syntaxes are all correct.

<h:form>
    <label style="font-weight: bold">Room Code: </label>#{r.code}
    <label style="font-weight: bold">Room Type: </label>#{r.roomType}
    <label style="font-weight: bold">Price Per Day: </label>#{r.pricePerDay}
    <br/>
    <h:commandButton value="Check" action="#{bookingController.checkRoom}">
        <f:param name="selectedRoomID" value="#{r.id}"/>
        <f:ajax execute="@form" render开发者_Python百科="informer"/>
    </h:commandButton>
    <h:panelGroup id="informer">
        <h:outputText value="#{bookingController.message}" rendered="#{bookingController.checked}"/>
        <h:commandButton value="Book now!" action="#{bookingController.doBook}" rendered="#{bookingController.goodToBook}">
            <f:param name="selectedRoomID" value="#{r.id}"/>
            <f:param name="customerID" value="#{customerLoginController.customerUser.customer.id}"/>
        </h:commandButton>
    </h:panelGroup>
</h:form>


You've a rendered attribute on the second <h:commandButton>. If it evaluates false during processing of the form submit, then the button's action won't be invoked. This will happen if the bean is request scoped and you don't preserve the same condition for the rendered attribute during the form submit request as it was during displaying the form.

There are several ways to go around this. Since you're using JSF 2.0, best is to put the bean in the view scope instead.

@ManagedBean
@ViewScoped
public class BookingController {
    // ...
}

See also:

  • commandButton/commandLink/ajax action/listener method not invoked or input value not updated
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜