How to remove specific FacesMessage from FacesCont

2019-09-05 02:06发布

How can we delete a specific FacesMessage from FacesContext. Lets say I have list which contains two FacesMessages whose detail(getDetail()) is Mandatory Fields and Invalid Account Number and I want to delete Mandatory Fields FacesMessage.

Below is my sample code :

ListIterator<?> cmessages = context.getMessageList().listIterator();
cmessages.hasPrevious();
while (cmessages.hasNext() && cmessages!=null) {

    FacesMessage msg =(FacesMessage) ((cmessages.next() instanceof FacesMessage)?cmessages.next():null);
    if(msg!=null){
        if(msg.getDetail().equals(messageDetail)){
              cmessages.next();
              cmessages.remove();
        }
     }
}

As my above code also giving me sometimes UnsupportedOperation exception

1条回答
一纸荒年 Trace。
2楼-- · 2019-09-05 02:50

Below is code snippet I wrote that fixed my issue

public static void clearMessagesWithID(String messageID)
{
  String messageDetail = getErrorMessage(messageID);
  FacesContext context = FacesContext.getCurrentInstance();
  for (Iterator<FacesMessage> iterator = context.getMessages(); iterator.hasNext();) {
    FacesMessage msg = iterator.next();
    if (msg.getDetail().contains(messageDetail)) {
      // Remove the current element from the iterator and the list.
      iterator.remove();
    }
  }
}

And that UnsupportedOperation was reported because I was previously trying to access element unexistent element in list

查看更多
登录 后发表回答