Phani Ramaraju
Phani Ramaraju

Reputation: 33

Unable to retrieve the value of <h:selectBooleanCheckbox> inside my managed bean's action method

JSF view code:

<f:view>
    <h:form>
        <h:panelGrid>
            <h:inputText id="key" value="#{myManagedBean.key}"/>
            <h:selectBooleanCheckbox id="rerun" value="#{myManagedBean.rerun}" rendered="#{myManagedBean.displayRerun}"/>
            <h:commandButton id="check" action="#{myManagedBean.check}"/>
        </h:panelGrid>
    </h:form>
<f:view>

JSF model code:

public class MyManagedBean {

    private boolean displayRerun;


    public void setDisplayRerun(boolean aDisplayRerun) {
       this.displayRerun = aDisplayRerun        }

    public boolean getDisplayRerun() {
       return this.displayRerun;
    }

   private String key;


    public void setKey(String aKey) {
       this.key = aKey
    }

    public String getKey() {
       return this.key;
    }
    private boolean rerun;


    public void setRerun(boolean arerun) {
       this.rerun = arerun
    }

    public boolean getRerun() {
       return this.rerun;
    }

    public String check() {
        //do data validation
        setDisplayRerun(true);
        System.out.println(getRerun());
    }

}

This always prints false regardless of whether the checkbox is checked or not.

Additional Information on my requirement:

Nick/BalusC, my managed bean is of request scope. It is indeed simplified code snippet that I presented. My page has couple of user input controls along with a command button. On submit of command button, I call action method of backing bean, in which I do data validation (in this case I lookup database and see if the inputs are already registered.) If already registered, I come back to the same page, this is when I display the singleBooleanCheckBox for the user to select and hit the command button again. I am toggling the display of the checkbox based on a managedbean property (a boolean flag set during data validation).

When I re-submit the page with checkbox checked, I do not receive this data.

For further verification, I replace the selectBooleanCheckbox, with a command button with similar behavior (basically do not render it initially, but only show it on data validation). I mapped its @action to my managedbean's action method. To my surprise, when I hit the button, the action method is not executed. Instead, the page is refreshed like in a "immediate" scenario or a redirect.

I have been struggling on this for almost 6 hrs. Appreciate your experienced insights.

Thanks.

Upvotes: 3

Views: 6478

Answers (1)

BalusC
BalusC

Reputation: 1108722

So, you've actually a rendered attribute on the checkbox (that was not present in your initial question) and the bean is request scoped (it would have worked when it was session scoped). The submitted checkbox value will not be gathered during apply request values phase when this attribtue evaluates false at that point.

You basically need to retain the condition responsible for the rendered attribute in the subsequent request as well. This can be done in several ways: putting bean in session scope, using Tomahawk's t:saveState or h:inputHidden with a bean binding. Each is outlined in detail in this answer.

Upvotes: 1

Related Questions