Search code examples
jsfhttp-redirectjsf-2.2omnifacesconversation-scope

Omnifaces Faces.redirect loses conversation scope


I have problem with org.omnifaces.util.Faces#redirect and conversation scoped bean:

there is a button

<p:commandButton action="#{navigationHandler.gotoCreateCar}"
  actionListener="#{createHandler.init(searchHandler.search())}
  value="#{msg.search}" update=":articleSearchForm">
  <f:param name="cid" value="#{javax.enterprise.context.conversation.id}"/>
</p:commandButton>

which must do a navigation to createCar page within the same conversation scope after init of my conversation scoped bean: createHandler.

In the NavigationHandler#gotoCreateCar is just a call of Faces.redirect(createCarPage).

If I do like this the parameter cid is not transfered and I lose my conversation.

If I define a navigation rule in faces-config.xml:

<navigation-case>
  <from-outcome>createCar</from-outcome>
  <to-view-id>/portal/createCar.xhtml</to-view-id>
  <redirect />
</navigation-case>

and in the NavigationHandler#gotoCreateCar just return the needed outcome - then it works fine.

Maybe I do not understand every detail in the difference between this two navigation approaches. I would be appreciated if somebody could help me to understand the problem.

Thanks!


Solution

  • The conversation propagation is handled by the navigation handler. The Faces#redirect() delegates to ExternalContext#redirect() which does not use the navigation handler. You'd better use Faces#navigate() instead which delegates to NavigationHandler#handleNavigation().

    public void gotoCreateCar() {
        // ...
    
        Faces.navigate("/portal/createCar.xhtml?faces-redirect=true");
    }
    

    (note: no <navigation-case> is needed in this case)

    Alternatively, just return exactly that string from the action method.

    public String gotoCreateCar() {
        // ...
    
        return "/portal/createCar.xhtml?faces-redirect=true";
    }    
    

    The Faces#navigate() is only useful when you're inside a (listener) method which doesn't support returning a navigation case outcome, such as @PostConstruct or preRenderView.