2012-03-28 42 views
2

我有一個對話框,用戶可以添加一個項目的頁面。我希望服務器端驗證錯誤消息顯示在p:message標記中,但是如果驗證通過,我希望成功消息顯示在p:growl標記中並關閉對話框。primefaces試圖添加一個p:消息的驗證錯誤和p:咆哮的成功消息

我該如何做到這一點?目前,我可以將所有消息(錯誤或成功)都顯示在所有p:growl,所有p:消息中,或全部都顯示在兩者中。

<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); 

} 
+0

不完全是你在找什麼......但...看看這個答案... stackoverflow.com/a/8634383/617373 – Daniel 2012-03-29 09:16:43

回答

1

利用 「globalOnly」 功能。在FacesContext#addMessage()調用中,爲所有錯誤消息提供非客戶端ID(例如,輸入組件之一,或按鈕組件或甚至表單組件),併爲所有成功消息提供客戶端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); 
}