本文介绍了如何从FacesContext中删除特定的FacesMessage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们如何从FacesContext中删除特定的FacesMessage.可以说我有一个包含两个FacesMessages的列表,它们的详细信息(getDetail())是Mandatory FieldsInvalid Account Number,我想删除Mandatory Fields FacesMessage.

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.

下面是我的示例代码:

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

由于我上面的代码有时还会给我UnsupportedOperation异常

As my above code also giving me sometimes UnsupportedOperation exception

推荐答案

下面是我编写的可修复问题的代码段

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

报告了该UnsupportedOperation,因为我以前试图访问列表中不存在的元素

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

这篇关于如何从FacesContext中删除特定的FacesMessage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 17:51