如何检查组件是否已存在p:消息?

时间:2012-12-21 06:29:19

标签: jsf-2 primefaces

我的问题是,我们可以通过哪种方式检查特定Primefaces组件是否已存在消息,如果没有,则只为该组件添加消息。

1 个答案:

答案 0 :(得分:2)

您可以通过FacesContext对象访问特定组件的排队消息。以下代码应该有效:

     FacesContext context = FacesContext.getCurrentInstance(); //obtain a reference to the FacesContext
     String desiredClientId = "componentId"; //You should already have the client side id of the component you want to operate with
     Iterator<FacesMessage> messageQueue =  context.getMessages(desiredClientId); //Obtain an Iterator for a List of possible queued messages for the component id you've provided.
     if(messageQueue.hasNext()){
      //the component has messages queued, do whatever you want
      }
      else{
      no messages, do whatever you want
      }