Primefaces隐藏和取消隐藏对话框

时间:2012-11-13 04:53:12

标签: primefaces

现在我正在研究用户界面令人难以置信的素数,我想用它来构建我的网站,所以现在我正在研究对话框,我想使用primefaces的requestcontext隐藏和取消隐藏对话框我该怎么做到这一点?我在stackoverflow中搜索同样的问题,我发现了一些,但它不起作用,为了使一切都清楚,这是我的register.jsf代码:

  <?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
    <title>Insert title here</title>
    <h:outputStylesheet library="css" name="style.css"/>
</h:head>
<h:body>
    <p:panel toggleable="true" header="Registration" closable="true" closeTitle="back" styleClass="register-panel">
        <h:form>
            <h:panelGrid columns="2">
                <h:outputLabel  value="Firstname: " />
                <p:inputText id="firstname" value="#{ register.firstName }" required="true" requiredMessage="Firstname cannot be blank">
                    <f:validateDoubleRange minimum="3" maximum="15"  />
                </p:inputText>

                <h:outputLabel value="Middlename: " />
                <p:inputText id="middlename" value="#{ register.middleName }" required="true" requiredMessage="Middlename cannot be blank">
                    <f:validateDoubleRange minimum="3" maximum="15"  />
                </p:inputText>

                <h:outputLabel value="Lastname: " />
                <p:inputText id="lastname" value="#{ register.lastName }" required="true" requiredMessage="Lastname cannot be blank">
                    <f:validateDoubleRange minimum="3" maximum="15"  />
                </p:inputText>

                <p:commandButton type="button" update="errorFirst, errorMiddle, errorLast, ErrorD, confirmD" value="Register" actionListener="#{ register.Register }" />

                <p:dialog width="350"  modal="true" id="ErrorD" header="Error Registration" visible="true" maximizable="false"  widgetVar="showErrorDialog">
                <h:panelGrid columns="1">
                    <p:message for="firstname" id="errorFirst" />
                    <p:message for="middlename" id="errorMiddle" />
                    <p:message for="lastname" id="errorLast" />
                </h:panelGrid>
                </p:dialog>

                <p:dialog width="350" modal="true" id="confirmD" header="Confirm Registration"  visible="true" maximizable="false"  widgetVar="showConfirmDialog">
                    <h:panelGrid columns="2">
                        <h:outputLabel value="Are you sure you want to save the records" />
                        <h:outputLabel value=""/>
                        <p:separator />
                        <p:separator />

                        <p:commandButton value="Ok, Sure" />
                        <p:commandButton value="Cancel" />
                    </h:panelGrid>

                </p:dialog>

            </h:panelGrid>

        </h:form>
    </p:panel>
</h:body>
</html>

这是我的寄存器bean:

    package org.register;

import javax.annotation.PostConstruct;
import javax.faces.bean.*;
import javax.faces.event.ActionEvent;
import org.primefaces.context.RequestContext;

@ManagedBean(name="register")
@RequestScoped
public class registerBean {

    private String firstName;
    private String middleName;
    private String lastName;

    public String getFirstName() {
        return firstName;
    }
    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }
    public String getMiddleName() {
        return middleName;
    }
    public void setMiddleName(String middleName) {
        this.middleName = middleName;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void Register(ActionEvent e){
        RequestContext primeContext = RequestContext.getCurrentInstance();
        if(getFirstName().equals("") || getMiddleName().equals("") || getLastName().equals("")){
            primeContext.execute("showErrorDialog.show()");
        }
        else{
            primeContext.execute("showConfirmDialog.show()");
        }

    }


}

我的代码没有任何错误,但问题是每次页面加载时,它都调用了2对话框,我要做的是打开第一个对话框,如果注册失败,打开第二个对话框,当它是成功。谢谢你们。

1 个答案:

答案 0 :(得分:0)

基本上,代码应该通过删除注册type="button"中的commandButton来工作,以便使用ajax请求。甚至没有调用方法#{register.Register}。 同时删除两个对话框中的visible="true"

作为旁注,为了执行支持方法,JSF执行验证规则,因此只有当表单中的所有数据都满足此类验证时,您才会看到confirmD对话框