Faces messages are not cleared on subsequent requests

谁说我不能喝 提交于 2020-01-16 01:05:48

问题


Case is as follows:

you have a bean method which parses a file, and if parsing fail, error message is added, and if parsing successful, success message is added.

But when you make consecutive operations: fail > success , i expect that the fail message will disappear and the success message appears, but what happens is that fail message is still there, and success message is added to it.

Clearing the MessageList before adding the message is not a solution, because list is already cleared, if you try to print the message list size before adding the message in both cases it will be 0.

So what is the solution to remove fail message in case of success and vice versa?

Bean:

@Component("mybean")
@Scope("view")
public class MyBean {

    try {
        myservice.parseFile(file);
    } catch (Exception e) {
        FacesMessage msg = new FacesMessage();
        msg.setSeverity(FacesMessage.SEVERITY_FATAL);
        msg.setSummary("Invalid file.");
        facesContext.addMessage(null, msg);
        return;
    }

    FacesMessage msg = new FacesMessage();
    msg.setSeverity(FacesMessage.SEVERITY_INFO);
    msg.setSummary("Success");
    facesContext.addMessage(null, msg);

}

View:

<h:form>
    <ace:fileEntry id="fileEntryComp"
        label="File Entry"
        relativePath="uploaded"
        fileEntryListener="#{mybean.listener}" /> 

    <h:commandButton value="Upload File" />
    <h:messages  styleClass="myclass" infoStyle="Color:blue;" errorStyle="Color:red;" fatalStyle="margin-right: 85%; Color:red;" globalOnly="true"/> 
    <h:messages for="fileEntryComp" style="display:none;"/> <!-- to hide the faces development message-->     
</h:form>

UPDATE:

i tried even the workaround here:

Is is possible to delete Component HTML Content with JSF

to clear the messages div before adding new messages, but no new, i don't know where he gets the old message from.

UPDATE2:

i even tried the two workaround mentioned here:

http://www.icefaces.org/JForum/posts/list/19753.page#71521

1- Adding context param:

<context-param>
    <param-name>org.icefaces.messagePersistence</param-name>
    <param-value>false</param-value>
</context-param> 

doesn't work too.

2- Clearing saved global messages collection:

i tried this solution:

 List<FacesMessage> globals = (List<FacesMessage>) facesContext.getViewRoot().getAttributes().get("org.icefaces.event.saved_global_faces_messages");
 if (globals != null) {
     globals.clear();
 }

but i always get the following exception:

Caused by: java.lang.UnsupportedOperationException
    at java.util.Collections$UnmodifiableCollection.clear(Collections.java:1037)
    at com.xeno.phoneSuite.beans.DepartmentBean.listener(DepartmentBean.java:176)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
    at org.icefaces.component.fileentry.FileEntry.broadcast(FileEntry.java:311)
    ... 92 more

回答1:


finally, i tried the solution of the context param on the latest version ICEfaces 2.1 Beta 2 and it works fine:

<context-param>
        <param-name>org.icefaces.messagePersistence</param-name>
        <param-value>false</param-value>
  </context-param>

http://wiki.icefaces.org/display/ICE/ICEfaces+2.1.0+Beta+2+Release+Notes#ICEfaces2.1.0Beta2ReleaseNotes-downloads

hope that will helps.




回答2:


I am in Icefaces 3.3 and had a similar problem. I solved this using the following code (Oh BTW I also see that @BalusC has already pointed out such a solution in his comments):

Iterator<FacesMessage> msgIterator = FacesContext.getCurrentInstance().getMessages();
while (msgIterator.hasNext())
{
    FacesMessage facesMessage = msgIterator.next();
    msgIterator.remove();
}

Putting above piece of code before pushing a new message like this one, should clear your old message and replace it with the new message My Message:

FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "My Message", null));



回答3:


I struggled a lot and finally the fix was very simple whether it is prime faces or old faces, just add redisplay = false.

Below is the snippet of the code.

<p:messages id="globalMessages" global-only="true" redisplay="false">
   <p:autoUpdate/>
</p:messages>


来源:https://stackoverflow.com/questions/8215720/faces-messages-are-not-cleared-on-subsequent-requests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!