2

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
Community
  • 1
  • 1
Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
  • 1
    Your problem is likely caused by the way how you present the messages. Show it. – BalusC Nov 21 '11 at 17:49
  • Well, that'll be related to IceFaces+Ajax. Sorry, I have no idea. – BalusC Nov 21 '11 at 18:34
  • @Bhesh Gurung , question updated. – Mahmoud Saleh Nov 26 '11 at 17:28
  • With regard to the exception in your 2nd update: you can indeed not delete faces messages like that. It has to be done by `Iterator#remove()` method on the `Iterator` which is returned by `FacesContext#getMessages()` for example. – BalusC Nov 26 '11 at 17:31
  • i can't get you, can you please explain with code sample ? – Mahmoud Saleh Nov 26 '11 at 17:36
  • @BalusC some people at icefaces forum advised to implement a phaselistener at the end of the life cycle to clear the faces messages, can you please give a little advise about implementing that phase listener ? does this mean the prerender javax.faces.event.PreRenderComponentEvent or what ? please advise since i am new to phaselisteners – Mahmoud Saleh Dec 05 '11 at 16:37
  • @BalusC sure in a separate question, already done :) – Mahmoud Saleh Dec 05 '11 at 16:53
  • I see, sorry I have no idea how to clear IceFaces specific messages. You by the way already seemed to have solved it, seeing your answer below. – BalusC Dec 05 '11 at 17:05
  • @BalusC yes, problem is solved in the latest beta version which have some other problems, so i am trying to solve it in the latest release, anyway i just need a very simple example about phase listener at the end of the lifecycle and i will try to figure the rest out. – Mahmoud Saleh Dec 05 '11 at 17:08

3 Answers3

2

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.

Mahmoud Saleh
  • 33,303
  • 119
  • 337
  • 498
0

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));
Rash
  • 7,677
  • 1
  • 53
  • 74
0

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>
SARIKA
  • 29
  • 2