Nils Otto
Nils Otto

Reputation: 611

JSF 2.0 Flash-scope - cookie path

I have been struggeling with the Flash-scope (I know it is not a "scope" like the others, but often referred to as one) in JSF 2.0. (Mojarra)

My problem is that the flash-cookie is bound to the path of the view putting the object in flash.
This resulting in objects not being available after redirect to a different path in the same application. We need to have Strings (and probably objects) available on the other side of the redirect-navigation.

I have implemented a @WebFilter which purpose is to override this and replace the ServletResponse with the following wrapper.

private class ResponseWrapper extends HttpServletResponseWrapper{

    private final String path;

    public ResponseWrapper(HttpServletResponse response, String contextpath) {
        super(response);
        this.path = contextpath;
    }

    @Override
    public void addCookie(Cookie cookie) {

        // Hardcoded name from jsf-impl # com.sun.faces.context.flash.ELFlash  
        final String FLASH_COOKIE_NAME = "csfcfc";

        if (cookie.getName().equals(FLASH_COOKIE_NAME)){
            cookie.setPath(path);
        }
        super.addCookie(cookie);
    }

}

In effect this wrapper will modify the flash-cookies to be bound to the applications context-root.

My question is whether this will cause other problems that I do not oversee.
I am not able to understand why the Flash should be bound to the path of the view in the first place.

Upvotes: 1

Views: 3560

Answers (2)

Jonathan S. Fisher
Jonathan S. Fisher

Reputation: 8817

One of your assumptions is incorrect:

is that the flash-cookie is bound to the path of the view putting the object in flash.

As it turns out, Flash scope survives redirect, exactly once.

In other words, objects placed in Flash scope will survive a redirect. http://mkblog.exadel.com/2010/07/learning-jsf2-using-flash-scope/

If you're messing with cookies and filters, you're probably doing something wrong. JSF abstracts those concepts away so you can just use POJOs to do your work. Don't mess with Java EE cookies, you're going to do a lot more work than you need to and you instantly create very fragile code.

To use the flash scope, grab a reference to it like this:

Flash flash = FacesContext.getCurrentInstance().getExternalContext().getFlash();

To put:

flash.put("myObject", myObject);

to Get:

Object myObject = flash.get("myObject");

Will your hack cause problems with JSF? I'd say with definite certainty, if not now, then later when you try and upgrade Mojarra, use a different container, or switch to MyFaces.

Upvotes: 2

Yasei No Umi
Yasei No Umi

Reputation: 1574

This doesn't really answer your question, but looking at this questions and notes - After post is setting a value in JSF2 flash scope, it is visible again on second GET request to a page (Flash scope considered harmful)

Maybe you should use RenderScoped instead (if applicable)?

Upvotes: 0

Related Questions