后续请求中不会清除 Faces 消息

发布于 2024-12-17 12:42:22 字数 3304 浏览 1 评论 0 原文

情况如下:

你有一个解析文件的bean方法,如果解析失败,则添加错误消息,如果解析成功,则添加成功消息。

但是当你连续操作时:fail > success ,我希望失败消息会消失并出现成功消息,但发生的情况是失败消息仍然存在,并且成功消息被添加到其中。

在添加消息之前清除 MessageList 不是一个解决方案,因为列表已经被清除,如果您尝试在添加消息之前打印消息列表大小,在这两种情况下它都将是 0。

那么在这种情况下删除失败消息的解决方案是什么成功,反之亦然?

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

}

查看:

<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>

更新:

我什至尝试了这里的解决方法:

可以使用 JSF 删除组件 HTML 内容

以在添加新消息之前清除消息 div,但没有新消息,我不知道他从哪里得到旧消息。

更新2:

我什至尝试了这里提到的两种解决方法:

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

1- 添加上下文param:

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

也不起作用。

2-清除保存的全局消息集合:

我尝试了这个解决方案:

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

但我总是遇到以下异常:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

只涨不跌 2024-12-24 12:42:22

最后,我在最新版本ICEfaces 2.1 Beta 2上尝试了上下文参数的解决方案,它工作正常:

<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

希望有所帮助。

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.

揽月 2024-12-24 12:42:22

我在 Icefaces 3.3 中也遇到了类似的问题。我使用以下代码解决了这个问题(哦,顺便说一句,我还看到 @BalusC 已经在他的评论中指出了这样的解决方案):

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

在推送像这样的新消息之前放置上面的代码,应该清除旧消息并替换它新消息我的消息

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

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));
浅语花开 2024-12-24 12:42:22

我费了很大劲,最后修复很简单,无论是素脸还是旧脸,只需添加 redisplay = false 即可。

下面是代码片段。

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文