How to remove a specific FacesMessage from a FacesContext?

How to remove a specific FacesMessagefrom FacesContext. Suppose I have a list containing two FacesMessages whose detailed information ( getDetail()) is equal to Mandatory Fieldsand Invalid Account Number, and I want to delete Mandatory Fields FacesMessage.

The following is 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();
        }
     }
}

Like my code above also sometimes gives me an exception UnsupportedOperation

0
source share
1 answer

The following is a snippet of code. I wrote that I fixed my problem

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 was reported UnsupportedOperationbecause earlier I tried to access an item that is not an item in the list

0
source

Source: https://habr.com/ru/post/1534604/


All Articles