java - primefaces 试图添加 p :message for validation errors and p:growl for success messages

标签 java jsf primefaces

我有一个带有对话框的页面,用户可以在其中添加项目。我希望服务器端验证错误消息显示在 p:message 标记中,但如果验证通过,我希望成功消息显示在 p:growl 标记中并关闭对话框。

我怎样才能实现这个目标?目前,我可以让所有消息(错误或成功)出现在所有 p:growl、所有 p:messages 或两者中。

<p:growl id="growl" showDetail="true" life="3000" autoUpdate="false" />

<p:dialog id="projectsDialog" header="Add Project" widgetVar="projectsDialog"  modal="true">

  <p:messages id="messages" autoUpdate="false" showDetail="true" />

    <h:form>

      <h:outputLabel for="investigator" value="Investigator" />
      <p:selectOneMenu value="#{ppBacker.investigatorKey}" id="investigator" size="1" styleClass="listBoxMedium" >
      <f:selectItems value="#{ppBacker.investigators}" />
      </p:selectOneMenu>            

      <h:outputLabel for="startDate" value="Start Date" />
      <p:calendar value="#{ppBacker.startDate}" id="startDate" mode="popup" navigator="true" required="true" requiredMessage="Start Date is required" />                    

      <h:outputLabel for="endDate" value="End Date" />
      <p:calendar value="#{ppBacker.endDate}" id="endDate" mode="popup" navigator="true" required="true" requiredMessage="End Date is required" />

      <p:commandButton value="Add Project" id="s1Btn" type="submit" oncomplete="handleLoginRequest(xhr, status, args)" actionListener="#{ppBacker.addProject}" update=":growl">
      <f:setPropertyActionListener target="#{ppBacker.grantKey}" value="#{ppBacker.grantKey}" />
      </p:commandButton>           

      <!-- this jquery closes the add project dialog if the server side validation is successful -->
      <script type="text/javascript">  
        function handleLoginRequest(xhr, status, args) {  
            console.log('args.success = '+args.success);
            console.log('args.validationFaile = '+args.validationFailed);
                if(args.validationFailed || !args.success) {  
                  jQuery('#projectsDialog').effect("shake", { times:1 }, 100);  
            } else {  
              projectsDialog.hide();  

            }  
        }  
    </script>  

    </h:form>

我的 ppBacker.addProject 方法

public void addProject(){

    RequestContext context = RequestContext.getCurrentInstance();  
        FacesMessage msg = null;  
        boolean success = false;  

        // validate the investigator, start date, and end date are set
        if(!getProject().getInvestigatorKey().equals("0") && getProject().getStartDate() != null && getProject().getStartDate() != null) {  
            success = true;
        msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Project Added Successfully", ""); 

        } else {  
            success = false;    
            msg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Validation Failed", "Investigator, Start Date, and End Date are required");  

        }  

        System.out.println("success = "+success);
        // I'M GUESSING THAT I CAN SOMEHOW USE THE FIRST PARAMETER OF THIS TO ATTACH MY MESSAGE, BUT I'M NOT SURE
        FacesContext.getCurrentInstance().addMessage(null, msg);
        context.addCallbackParam("success", success);  

}

最佳答案

利用“globalOnly”功能。在 FacesContext#addMessage() 调用中,为所有错误消息提供一个非 null 客户端 ID(例如输入组件、按钮组件甚至表单组件) ) 并为所有成功消息指定 null 客户端 ID。

这样你就可以逃脱:

<p:messages ... globalOnly="false" />
<p:growl ... globalOnly="true" />

另一种方法是依赖 rendered 属性中的#{facesContext.validationFailed}。手动添加错误消息时,调用FacesContext#validationFailed()

if (error) {
    context.addMessage(null, errorMessage);
    context.validationFailed();
} else {
    context.addMessage(null, successMessage);
}

关于java - primefaces 试图添加 p :message for validation errors and p:growl for success messages,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9911186/

相关文章:

java - JSF 中每个用户/ session 的唯一标识符

java - 如何不用浏览器后台打开一个有IP的url?

java - NoClassDefFoundError - 动态加载类路径类依赖项在 URLClassLoader 中加载

java - 我如何创建一组 h :selectOneRadio in JSF?

java - JSF - UISelectItems 问题

jsf-2 - JSF 中的 UTF-8 表单提交会损坏数据

java - 我如何通过推送 primeFaces 来刷新 dataTable

jsf - 如何在 Primefaces 数据表中自定义分页

java - 如何在取消订阅时取消异步 Observable 线程

ios - 在 JSON 中获取无效的顶级类型写入 iOS