Henry
Henry

Reputation: 37

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

Upvotes: 1

Views: 1870

Answers (1)

BalusC
BalusC

Reputation: 1108632

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:

Upvotes: 1

Related Questions