开发者

JSF 2 f:ajax lifecycle problem

开发者 https://www.devze.com 2022-12-30 23:03 出处:网络
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.

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 u开发者_StackOverflow社区pdate of the first and last properties.

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


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.

0

精彩评论

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