2012-05-16 30 views
4

在我的Facelets頁我有這樣的:如何指定消息是否顯示在p:growl或p:messages中?

<p:growl id="msg1" life="1500"/> 

和另一

<p:messages id="msg2"/> 

我需要在下面留言僅<p:messages>露面。

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("veillez saisir les champs obligatoires (*)", null)); 

但它也出現在<p:growl>

如何指定郵件顯示的位置?

回答

14

由於p:messages只是h:messages的延伸,p:growlh:messages實際上是相同的東西,你不能。你可以做的是在你添加一條消息到context(可能你在做一些「確認」commandButton)的時候不更新p:growl,那麼它根本不會顯示,但是你不能指定只顯示一些消息。更好的解決方案是不要混合使用p:growlp:messages並只使用一個。

你正在尋找的功能將在新Primefaces 3.3Targetable messages

+0

如果我有很多'p:messages'?誰選擇一個? – Youssef

+0

你顯然不知道它是如何工作的。 'p:messages'是一個顯示所有生成消息的組件。如果您想爲特定操作生成特定消息,請改爲使用「p:message」。看看Primefaces showcase [示例](http://www.primefaces.org/showcase/ui/messages.jsf),不要將'p:message'與'p:messages'混淆。屬性'for'是關鍵。 – Fallup

+0

關鍵是什麼?你能舉個例子嗎?因爲我想添加這樣的消息:'FacesContext.getCurrentInstance()。addMessage' – Youssef

2

既然你已經分配2組不同的ID爲<p:growl><p:messages>,我想你可以嘗試這樣可供選擇:

<div id="aDiv"> 

    ### Use the following button to update only <p:growl id="msg1"> ### 
    <p:commandButton actionListener="#{mrBean.doSomething}" update="msg1" /> 

    ### Use the following button to update only <p:messages id="msg2"> ### 
    <p:commandButton actionListener="#{mrBean.doSomethingElse}" update="msg2" /> 

</div> 

關鍵是你應該只更新msg1msg2,而不是兩者。在上面的示例中,如果您的按鈕的屬性爲update="aDiv",則您的郵件將顯示在<p:growl><p:messages>上。

+0

正如我所說,他可以選擇更新什麼來重新渲染這些組件之一,但他的原始問題是他是否可以選擇哪些消息將顯示在哪個組件中,這是不可能的(將在PF 3.3中)。 – Fallup

14

從primefaces手冊中提取。第282

可指定信息

有可能是你需要一個或多個消息定位到特定的消息 組件,例如,假設你有咆哮和同一頁面上的信息時代,你需要 在咆哮和某些消息上顯示一些消息。使用for屬性將消息 與特定組件關聯。

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

FacesContext context = FacesContext.getCurrentInstance(); 
context.addMessage("somekey", facesMessage1); 
context.addMessage("somekey", facesMessage2); 
context.addMessage("anotherkey", facesMessage3); 

在上述樣品中,消息將顯示的第一和第二消息和咆哮將只顯示 第三消息。

0

它在 JSF + PrimeFaces 5.2

<p:messages for ="Message1" showDetail="true" autoUpdate="true" closable="true" /> 

<p:messages for ="Message2" showDetail="true" autoUpdate="true" closable="true" /> 
容易,這裏的例子
FacesContext.getCurrentInstance().addMessage("Message1", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Hello 1")); 
FacesContext.getCurrentInstance().addMessage("Message2", new FacesMessage(FacesMessage.SEVERITY_ERROR, "Error!", "Hello 2")); 
1

您可以使用「嚴重性」 P的屬性:咆哮來指定要只在低吼顯示哪些類型的消息。

<p:growl id="messages" severity="info, warn, error, fatal" showDetail="true" life="5200" autoUpdate="true" /> 

現在,如果你想不使用低吼的廣播信息,只需刪除「信息」 perameter,然後你可以使用號碼:消息或您自己選擇的任何文字,樣式也相應<p:outputLabel value="A Validation error occured!" rendered="#{facesContext.validationFailed}" /> 所以在這樣你可以根據你的選擇使用咆哮和消息。

相關問題