0
votes

On page I have checkbox which enables/disables h:inputText and h:selectOneMenu. That works fine until I refresh the page. Value of checkbox is probably cached by browser.

So, when the checkbox was set on "true" after refresh both controls are enabled. selectOneMenu is rendered corresponding to value set in init() method, but selectBooleanCheckbox and inputText isn't. How can I fix that? I use Firefox browser.

Backing bean is simple, one property and one method - init in which value is set to false.

@ManagedBean(name="newBean")
@ViewScoped
public class NewBean implements Serializable {
    private boolean booleanValue;  

    public NewBean() {

    }   

    public void init()
    {
        booleanValue = true;
    }   

    public boolean isBooleanValue() {
        return booleanValue;
    }

    public void setBooleanValue(boolean booleanValue) {
        this.booleanValue = booleanValue;
    }
}

Page source:

<h:body>
    <f:metadata>
        <f:viewAction action="#{newBean.init()}"></f:viewAction>
    </f:metadata>

    <h:form>
        <h:outputText value="#{newBean.booleanValue}"></h:outputText>
        <br/>
        <h:selectBooleanCheckbox value="#{newBean.booleanValue}" id="selected">
            <f:ajax event="change"
                    render="enabledInput disabledInput enabledMenu"></f:ajax>
        </h:selectBooleanCheckbox>
        <br/>
        <h:inputText disabled="#{newBean.booleanValue}" id="enabledInput"></h:inputText><br/>
        <h:selectOneMenu disabled="#{!newBean.booleanValue}" id="enabledMenu">
            <f:ajax></f:ajax>
        </h:selectOneMenu>
    </h:form>
</h:body>

EDIT: Added some screenshots so it will be easier to understand the problem.

Correct: Page opened by user (checkbox is checked, and it corresponds to bean value, textbox is disabled, menu is enabled):

First Image

Correct: User unchecked checkbox (checkbox is unchecked, value on bean is false too, textbox is enabled, menu is disabled):

Second Image

ISSUE: after unchecking user refreshed page. Value on bean is true, but checkbox is still unchecked. Also, textbox and menu are BOTH enabled.

Third Image

1
Have you tried to make bean session scoped yet?peterremec
Thanks for answer. Added some screenshots. SessionScoped bean give the same effect.Krzysztof

1 Answers

-1
votes

Hey there I didn't quite understand your problem but I'd like to point two things.

<f:metadata>
<f:viewAction action="#{newBean.init()}"></f:viewAction>
</f:metadata>

Must be in the < h:form /> for the action to take effect.

And also if you want the method init to take effect before you create the new instance of the view add the annotation @PostConstruct or simply put the code " booleanValue = true; " inside the constructor of the view.

Hopefully I may be of help.