Removing JSF messages from the flash

冷暖自知 提交于 2019-11-29 02:05:37
Arjan Tijms

I did get the exact same problem on JBoss AS 6 with both Mojarra 2.0.3 and Mojarra 2.1.1. The flash should survive only one redirect, but in practice it not always does.

In an action method I have basically the same code:

// [add global faces message]
FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
return "/someView.xhtml?faces-redirect=true";

I've been seeing three symptoms of the problem:

  1. After doing a postback on someView, the flash faces message keeps being displayed
  2. After a clean request to a completely different page, the face message is also displayed
  3. After going again through the code that adds the faces message, you'll get two of the same messages on someView

Issue 2 can be explained by realizing that the flash scope is cookie based (at least in Mojarra). Relatively few people seem to understand the inherent problem with that. I've found this related question, but it has no answers: Is Flash scope free of race conditions?

Also, when trying to reproduce the problem I've got approximately a 2/3 chance of actually reproducing it. Sometimes it indeed only survives a single redirect, sometimes it's gone after a few refreshes (time based or so?) and sometimes it just sticks around and the only way to get rid of it is restarting my server(!).

Maybe I'm doing something wrong somewhere?

Observing the cookies being set, I noticed the followed:

After initially triggering the action that does the redirect:

POST Response
Set-Cookie csfcfc=_50Xfr

GET Response
Set-Cookie csfcfc=50Xfn_51Xfn

The thing to notice here is that the response from the GET request for the redirect again sets a cookie. You want think that for a flash scope the cookie would need to be deleted here.

If I submit a form on the view towards I'm redirected, the following happens:

POST Response
Set-Cookie csfcfc=_50Xfn

The message is now gone.

However, when I submit the form once more, the following happens:

POST Response
Set-Cookie  csfcfc=_52Xfn

And, the message is back :(

If I again submit the form:

POST Response

There is no more Set-Cookie header and the message in once again gone. If I continue submitting the form, the last cookie (csfcfc=_52Xfn) keeps being send to the server, but there are no more new cookies being set and no messages are displayed.

Edit:

During debugging I came across a critical section in ELFlash.java (Mojarra 2.0.3):

void saveAllMessages(FacesContext context) {
    // take no action on the GET that comes after a REDIRECT
    Map<Object, Object> contextMap = context.getAttributes();
    PreviousNextFlashInfoManager flashManager;
    if (null == (flashManager = getCurrentFlashManager(contextMap, true))) {
        return;
    }
    if (flashManager.getPreviousRequestFlashInfo().isIsRedirect()) {
        return;
    }
}

During the context of the GET request following the redirect, one would suppose isIsRedirect would return true, but it returned false. This could be a local problem, that I'm going to investigate further.

Interesting to note is perhaps that the cookie values actually have some meaning, according to the following:

 FirstTimeThru("f"),
 SecondTimeThru("s"),
 IsRedirect("r"),
 IsNormal("n");

So the first cookie (_50Xfr), is a FirstTimeThru and a IsRedirect.

Try setting property redisplay to false:

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