When defining a navigation-case, how can I make a generic reference to the page an action was triggered from?

712 views Asked by At

I would like to have this navigation case work on any page, but I can't find how to generically reference the origin page on a navigation rule in the tag to-view-id:

    <navigation-rule>
        <from-view-id>/public/login/login.xhtml</from-view-id>
        <navigation-case>
            <from-action>#{LoginBean.login}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>/public/login/login.xhtml</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>

The case translates to: after proper login on page login.xhtml, redirect to self, that is, login.xhtml

I would like to have something like:

   <navigation-rule>
        <navigation-case>
            <from-view-id>*</from-view-id>
            <from-action>#{LoginBean.login}</from-action>
            <from-outcome>success</from-outcome>
            <to-view-id>self</to-view-id>
            <redirect />
        </navigation-case>
    </navigation-rule>

So if, for example, the login action was triggered from a page with view-id mypage.xhtml, I get <to-view-id>mypage.xhtml</to-view-id> without defining a particular navigation rule for <from-view-id>mypage.xhtml</from-view-id>

In other words, I'd like to know if there is an equivalent of <from-view-id>*</from-view-id> for the tag <to-view-id>

My current solution is returning an outcome of null instead of "success" on LoginBean.login action success when a parameter redirectToSelfAfterLogin is defined, which keeps me in the view I called the action from.

(I also thought of having a redirectToAfterLogin parameter and redirect to self by default returning null on success instead, but this is more practical for my current scenario)

Anyway, I would like to know if there could also be a faces-config navigation rule based solution.

1

There are 1 answers

2
kolossus On BEST ANSWER

You really needn't set a navigation case, if your intention is to just redirect to the originally requested page. The originally requested page is available as a variable in the request object, with the key javax.servlet.forward.request_uri which is easily retrievable in your login bean using

   FacesContext ctxt = FacesContext.getCurrentInstance();
   String originalURI = ctxt.getExternalContext().getRequestMap("javax.servlet.forward.request_uri"); //the original uri

If you really prefer a navigation-config-based setting, you can directly access the requestScope object in there with

   <to-view-id>#{requestScope['javax.servlet.forward.request_uri']}</to-view-id>

But I won't recommend that; it's just ugly. That's decidedly page-level stuff you're burying in a config file