<p:growl>和<p:messages>位于同一页面</p:messages> </p:growl>

时间:2011-12-26 06:49:11

标签: jsf-2 primefaces

我希望使用<p:messages>来显示错误消息,使用<p:growl>显示成功消息。 在支持bean中:

FacesContext context = FacesContext.getCurrentInstance(); 
context.addMessage(null, new FacesMessage(title, msg));

但我发现无论我在辅助bean中添加了什么消息,<p:messages><p:growl>都会显示它。

任何建议。

6 个答案:

答案 0 :(得分:11)

growl's demo page of PrimeFaces中,他们提到:&#34; Growl只是替换了h:messages组件。&#34;。我担心您可能无法实现目标,因为咆哮也会在视图中显示所有FacesMessage。

但是,如果您反转您的要求 - 使用<p:growl>&amp;使用<p:message>显示成功的消息,您实际上可以实现如下:

<p:message id="successMsg" for="successMsg" />

@ManagedBean
@RequestScoped
public class MrBean {

   public void doSomething() {
      FacesContext context = FacesContext.getCurrentInstance(); 

      if (failed) {
         context.addMessage(null, new FacesMessage("Failed", "Sry boss! I have failed.")); 

      } else {
         context.addMessage("successMsg", new FacesMessage("Successful", "Hey boss! I did it!"));

      }
   }

}

答案 1 :(得分:10)

看起来像PrimeFaces 3.3,你可以做你想做的事情:

http://blog.primefaces.org/?p=1894

您现在可以:

<p:messages severity="error" />
<p:growl severity="info, warn" />

context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error Title", "Error Message"));
context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Success Title", "Success Message"));

答案 2 :(得分:9)

JSF:

<p:messages for="somekey" />
<p:growl for="anotherkey" />

豆:

context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces Rocks"));
context.addMessage("somekey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "Always bet on Prime"));a
context.addMessage("anotherkey", new FacesMessage(FacesMessage.SEVERITY_INFO,"Sample info message", "PrimeFaces is developed by Chuck Norris"));

这对我来说很好用!

答案 3 :(得分:1)

当只有验证错误时,您可以使用不呈现的p:growl。

<p:growl 
    id="growl" 
    sticky="true" 
    showDetail="true" 
    rendered="#{not facesContext.validationFailed}"/>

答案 4 :(得分:1)

这可能是个老问题,但也许这仍然是相关的:

我遇到了同样的情况,我使用每个标签中的for语句进行了解决

<p:growl id="someid" for="growl" />
<p:messages id="messageid" for="messages" />

和支持bean:

context.addMessage("growl", new FacesMessage("Successful", "to growl"));
context.addMessage("messages", new FacesMessage("Successful", "to messages"));

只是作为旁注,如果你添加autoupdate="true",那么消息将被重置为growl和消息(如果你向growl添加消息,消息将被清除)

答案 5 :(得分:0)

我为咆哮和消息添加了一个单独的消息面板,并在我提交表单时更新此面板,如下所示:

 <p:panel id="messages">
    <p:messages for="errorMsg" showDetail="true" />
    <p:growl for="infoMsg" showDetail="true" />
    <p:growl globalOnly="true" />
 </p:panel>

<p:commandButton id="submit" value="Submit" update="messages" actionListener="#{yourManagedBean.submit}" />

在ManagedBean中

     // display submit info - showDetail="true" in infoMsg will show details message
     FacesMessage infoMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Submit Info", "Your details info message." );
     FacesContext.getCurrentInstance().addMessage("infoMsg", infoMsg);

     // display error message - e is instance of Exception
     FacesMessage errMsg = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error Occured", e.getMessage());
     FacesContext.getCurrentInstance().addMessage("errorMsg", errMsg);


     // display some generic message (with no details in growl)
     FacesMessage genericMsg = new FacesMessage("Generic Message");
     FacesContext.getCurrentInstance().addMessage(null, genericMsg);

我使用Primefaces 5.0进行了测试。

享受!