【问题标题】:To display validation message in primefaces dialog from managed bean从托管 bean 在 primefaces 对话框中显示验证消息
【发布时间】:2014-01-29 12:31:25
【问题描述】:

我试图在对话框中显示在托管 bean 中完成的验证结果的消息,但在提交表单时它没有显示在对话框中。

请帮我解决这个问题

我的 JSF 页面 sn-p 对话框

<p:dialog header="Add LPC" id="lpcDlg" widgetVar="dlg" rendered="true"
          appendToBody="true" resizable="true" modal="true" height="320px"
          width="38%">
    <h:form id="addLpc">
        <div align="center" style="margin-bottom: 1px; padding-bottom: 1px;">
            <h:outputLabel value="Add New LPC"
                           style="font-color:#000000;font-weight:bold;font-size:24px;padding-bottom:1px;"></h:outputLabel>
        </div>

        <div align="center">

            <p:messages id="lpcDlgMsg" showDetail="false" autoUpdate="true"
                        closable="true" />
            <p:messages id="lpcDlgMsg2" for="lpcDlgMessage" showDetail="false"
                        autoUpdate="true" closable="true" />
            <h:panelGrid id="addLpcForm" columns="2" appendToBody="true">

                <h:outputText value="LPC ID" />
                <p:inputText id="lpcId" value="#{lpcBean.lpcId}" required="true"
                             requiredMessage="LPC ID is required">
                    <f:ajax event="blur" render="lpcDlgMsg" />
                </p:inputText>

                <h:outputText value="First Name" />
                <p:inputText id="firstName" value="#{lpcBean.firstName}" />
                .
                .
                .
                .
            </h:panelGrid>
        </div>
        <div align="center">
            <p:commandButton id="submitButton" value="Submit" ajax="true"
                             update=":lpcForm:lpcDataTable,addLpc"
                             action="#{lpcBean.formSubmit}" oncomplete="dlg.hide()" />
            <p:commandButton id="cancelButton" value="Cancel" 
                             onclick="dlg.hide()" />

        </div>
    </h:form>
</p:dialog>

id lpcDlgMsg2 的消息是我试图在提交时显示的消息。另一条消息在模糊时显示正确。

提交时调用的方法片段

public void formSubmit()

{
        if(resultSet.next())
        {

            int lpcIdCount=rs.getInt("lpcCount");
            if(lpcIdCount!=0)
            {
                FacesContext.getCurrentInstance().addMessage("lpcDlgMessage", new FacesMessage(FacesMessage.SEVERITY_ERROR," ", "Duplicate LPCID"));
                System.out.println("after display");
            }
            else
            {
                .
                .
                .
                .
            }

        }
    }   

}

【问题讨论】:

    标签: jsf-2 primefaces


    【解决方案1】:

    这是我的建议:

    <p:dialog header="Add LPC" id="lpcDlg" widgetVar="dlg" rendered="true"
              appendToBody="true" resizable="true" modal="true" height="320px"
              width="38%">
        <h:form id="addLpc">
            <div align="center">
                <p:messages id="lpcDlgMsg" showDetail="false" autoUpdate="true"
                            closable="true" />
                <h:panelGrid id="addLpcForm" columns="2" >
                    <h:outputText value="LPC ID" />
                    <p:inputText id="lpcId" required="true" />
                    <h:outputText value="First Name" />
                    <p:inputText id="firstName" required="true" />
                </h:panelGrid>
            </div>
            <div align="center">
                <p:commandButton id="submitButton" value="Submit" 
                                 oncomplete="if (!args.validationFailed){dlg.hide();}" />
                <p:commandButton id="cancelButton" value="Cancel" 
                                 onclick="dlg.hide()" />
            </div>
        </h:form>
    </p:dialog>
    

    注意,我已简化您的代码以避免使用托管bean。

    如果您希望托管 bean 执行验证,请使用 RequestContext 有条件地执行将关闭对话框并从 Submitoncomplete 的代码/strong> 按钮。

    if (success) {
        RequestContext.getCurrentInstance().execute("dlg.hide()");
    }else{
        //show all the messages you need here
    }
    

    【讨论】:

    • 嗨丹尼尔,在必要时调用托管 bean 中的方法,因为我正在对数据库进行一些检查,如果主键 (LPCID) 重复,则必须在对话框中显示错误消息,不允许提交.
    【解决方案2】:

    你必须先在 head 标签内添加这个 javascript

    function handleComplete(xhr, status, args) {
            if (!args.validationFailed) {
                dlg.hide();
            } else {
            }
        }
    

    更多变化是

    <p:commandButton id="submitButton" value="Submit" ajax="true"
      update=":lpcForm:lpcDataTable,addLpc :lpcDlgMsg2"
      actionListener="#{lpcBean.formSubmit}" oncomplete="handleComplete(xhr, status, args)" />
    

    action 到 actionListener 因为 action 有 String 返回类型。 这样可以正常工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-08-15
      相关资源
      最近更新 更多