1

现在我正在研究primefaces UI是令人难以置信的,我想用它来构建我的网站所以现在我正在研究对话框,我想使用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>

这是我的注册豆:

    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 对话框,我想要做的是在注册失败时打开第一个对话框,并在成功时打开第二个对话框。多谢你们。

4

1 回答 1

0

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

顺便说一句,为了执行支持方法,JSF 执行验证规则,因此confirmD只有当表单中的所有数据都完成此类验证时,您才会看到对话框出现

于 2014-03-27T03:38:06.633 回答