views:

251

answers:

1

The problem is, that if a property is changed during an f:ajax request and a binded panelGroup should be newly created depending on that changed value, the old value is used.

This code will explain the problem.

Here is the backingbean TestBean:

public String getFirst() {
        return first;
    }
    public void setFirst(String first) {
        this.first = first;
    }
    public String getLast() {
        return last;
    }
    public void setLast(String last) {
        this.last = last;
    }

    public String getName(){
        return first+" "+last; 
    }
public void setDynamicPanel(HtmlPanelGroup panel){ }

    public HtmlPanelGroup getDynamicPanel(){
        Application app = FacesContext.getCurrentInstance().getApplication();
        HtmlPanelGroup component = (HtmlPanelGroup)app.createComponent(HtmlPanelGroup.COMPONENT_TYPE);

        HtmlOutputLabel label1 = (HtmlOutputLabel)app.createComponent(HtmlOutputLabel.COMPONENT_TYPE);
        label1.setValue(" --> "+getFirst()+" "+getLast());
        component.getChildren().add(label1);
        return component;
    }

and now the jsf/facelet code:

<h:form id="form">
    <h:panelGrid columns="1">
        <h:inputText id="first" value="#{testBean.first}" />
        <h:inputText id="last" value="#{testBean.last}" />
        <h:commandButton value="Show">
            <f:ajax execute="first last" render="name dyn" />
        </h:commandButton>
    </h:panelGrid>
    <h:outputText id="name" value="#{testBean.name}" />
    <h:panelGroup id="dyn" binding="#{testBean.dynamicPanel}" />
</h:form>

After the page was initially loaded the outputText and panelGroup shows both "null" as first and last. But after the button is pressed, the outputText is updated well, but the the panelgroup shows again only "null". This is due to the problem, that the "binded method" dynamicPanel is executed before the update of the first and last properties.

how can workaround this behaviour or what is wrong with my code?

+1  A: 

If you add the attribute immediate="true" to your input elements, the values will be applied during the "Apply Request Values" phase, and hence be present before your action executes. You may or may not need the immediate attribute set to true on the commandButton as well.

Brian Leathem
Are there some drawbacks with this? Does using `immediate="true"` introduce some additional tricks needed or does it remove support for some thing X?
Tuukka Mustonen
It means you skip validation for that input. This can be bad if you need to correct data in the "immediate" backing bean property. For instance if you wanted to require that the input elements be integers or something like that.
Brian Leathem