更新嵌套的Primefaces元素不起作用

时间:2014-01-07 10:13:05

标签: java-ee jsf-2 primefaces xhtml

从昨天起我无法弄明白:

我在.xhtml中有这个表格

<h:body>
<h:form id="formToolbar">
    <p:growl id="messages" />
    <p:toolbar id="toolbar">
        <p:toolbarGroup align="left">
            <h:outputText value="Epimarket: occasion cars" />
            <p:separator />
        </p:toolbarGroup>
        <p:toolbarGroup align="right">
            <p:commandButton type="button" id="checkout" value="Checkout"
                onclick="PF('dlg3').show()" title="signup" icon="ui-icon-check"
                rendered="#{(not empty tableBean.droppedCars) and (loginBean.logged)}" />
            <p:commandButton type="button" id="signupLink" value="Sign up"
                onclick="PF('dlg2').show()" title="signup" icon="ui-icon-play"
                rendered="#{!loginBean.logged}" />
            <p:commandButton type="button" id="loginLink" value="Login"
                onclick="PF('dlg').show()" title="login" icon="ui-icon-script"
                rendered="#{!loginBean.logged}" />
            <p:commandButton type="button" id="logout" value="Logout"
                onclick="#{loginBean.doLogout}" title=""
                icon="ui-icon-circle-close" rendered="#{loginBean.logged}" />
        </p:toolbarGroup>
    </p:toolbar>
</h:form>
</h:body>

首次访问时,用户会看到登录注册按钮

当他点击它时会弹出一些对话框,当这些对话框完成后,他点击:

<p:commandButton id="loginButton" value="Login"
        update="growl :formToolbar:toolbar" actionListener="#{loginBean.login}"
                    oncomplete="handleLoginRequest(xhr, status, args)" />

* Growl2指代代码的另一部分。

我期望它会做的是,当点击提交命令按钮时,它会尝试再次渲染#toolbar中包含的按钮组,以便重新渲染它们。

我认为这个问题很糟糕,但我找不到如何让它起作用。

1 个答案:

答案 0 :(得分:2)

问题出现在您的托管bean中。

以下是您的说明中启发的简化代码示例:

视图

<h:form id="formToolbar">
    <p:growl id="messages" />
    <p:toolbar id="toolbar">
        <p:toolbarGroup align="left">
            <h:outputText value="Epimarket: occasion cars" />
            <p:separator />
        </p:toolbarGroup>
        <p:toolbarGroup align="right">
            <p:commandButton type="button" id="loginLink" value="Login"
                             onclick="PF('dlg').show()" title="login" icon="ui-icon-script"
                             rendered="#{!loginBean.logged}" />
            <p:commandButton id="logout" value="Logout"
                             action="#{loginBean.doLogout}" title=""
                             icon="ui-icon-circle-close" rendered="#{loginBean.logged}" update="messages,toolbar" />
        </p:toolbarGroup>
    </p:toolbar>
</h:form>

<p:dialog header="Enter Username/Password" widgetVar="dlg" resizable="false">  
    <h:form>
        <h:panelGrid columns="2" style="margin-bottom:10px">  
            <h:outputLabel for="username" value="Username:" />  
            <p:inputText id="username" value="#{loginBean.username}" />  

            <h:outputLabel for="passwd" value="Password:" />  
            <p:password id="passwd" value="#{loginBean.password}" />  
        </h:panelGrid>  

        <p:commandButton id="loginButton" value="Login" oncomplete="PF('dlg').hide();" 
                         update=":formToolbar:messages, :formToolbar:toolbar" action="#{loginBean.doLogin}"/>  
        <p:defaultCommand target="loginButton" />
    </h:form>
</p:dialog>

受管Bean

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean
@SessionScoped
public class LoginBean implements Serializable {

    private String username;

    private String password;

    private boolean logged;

    public void doLogin() {
        logged = true;
    }

    public void doLogout() {
        logged = false;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean getLogged() {
        return logged;
    }

    public void setLogged(boolean logged) {
        this.logged = logged;
    }

}