2013-01-15 46 views

回答

2

爲什麼要用<p:message/>

試試這個

<h:outputText value="#{fn:length(facesContext.messageList)}"/> 
0

在功能方面沒有內置此。例如,您可以添加inputHidden元素形成,併爲輸入隱藏字段添加p:message

<p:messages for="justForErrorCount"/> 
<h:inputHidden id="justForErrorCount"/> 

在你的支持豆你可以做一些檢查和添加消息:

if (FacesContext.getCurrentInstance().getMessageList() != null && 
     !FacesContext.getCurrentInstance().getMessageList().isEmpty()) { 

    String message = "You have " + 
     FacesContext.getCurrentInstance().getMessageList().size() + " errors"; 
    FacesContext.getCurrentInstance().addMessage("justForErrorCount", 
     new FacesMessage(FacesMessage.SEVERITY_ERROR, message); 
} 
3

如果你只是想計算特定的消息(例如只有錯誤)。你可以做這樣的事情:

@ManagedBean 
@RequestScoped 
public class MessageCount { 

    public int getFatal() { 
     return countMessages(FacesMessage.SEVERITY_FATAL); 
    } 

    public int getError() { 
     return countMessages(FacesMessage.SEVERITY_ERROR); 
    } 

    public int getWarn() { 
     return countMessages(FacesMessage.SEVERITY_WARN); 
    } 

    public int getInfo() { 
     return countMessages(FacesMessage.SEVERITY_INFO); 
    } 

    private int countMessages(FacesMessage.Severity severity) { 
     Iterator<FacesMessage> iterator = FacesContext.getCurrentInstance().getMessages(); 
     int count = 0; 
     while (iterator.hasNext()) { 
      FacesMessage msg = iterator.next(); 
      if (severity.compareTo(msg.getSeverity()) == 0) { 
       count++; 
      } 
     } 
     return count; 
    } 
} 

而在你的JSF頁面:

<h:outputText value="#{messageCount.error}"/> 
相關問題