JSF 2 f:ajax 生命周期问题

发布于 2024-09-02 19:44:16 字数 1731 浏览 4 评论 0原文

问题是,如果在 f:ajax 请求期间更改属性,并且应根据更改的值重新创建绑定的 panelGroup,则将使用旧值。

这段代码将解释这个问题。

这是 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;
    }

现在是 jsf/facelet 代码:

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

页面最初加载后,outputText 和 panelGroup 将“null”显示为第一个和最后一个。但是按下按钮后,输出文本更新良好,但面板组再次仅显示“null”。这是由于以下问题造成的:“绑定方法”dynamicPanel 在更新第一个和最后一个属性之前执行。

如何解决此行为或我的代码有什么问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2024-09-09 19:44:17

如果您将属性立即=“true”添加到输入元素,则这些值将在“应用请求值”阶段应用,因此会在您的操作执行之前出现。您可能也可能不需要将 commandButton 上的立即属性设置为 true。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文