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