Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make redirect using navigation-rule

A Redirect from Bean in JSF can be done by

externalContext.redirect("foo.xhtml");

But I want to use the rules, set in <navigation-rule/> (faces-config.xml) for <from-outcome/> && pass command line arguments.

externalContext.redirect("from-outcome?param=bar");

Is not working. How to do that?

like image 690
timmornYE Avatar asked Aug 29 '13 13:08

timmornYE


1 Answers

The ExternalContext#redirect() takes an URL, not a navigation outcome.

You need to return navigation outcomes from action methods declared to return String.

public String submit() {
    // ...

    return "from-outcome";
}

You can configure the navigation case to send a redirect by adding <redirect>.

<navigation-rule>
    <navigation-case>
        <from-outcome>from-outcome</from-outcome>
        <to-view-id>/foo.xhtml</to-view-id>
        <redirect>
            <view-param>
                <name>param</name>
                <value>bar</value>
            </view-param>
        </redirect>
    </navigation-case>
</navigation-rule>

Note that you can also just make use of JSF implicit navigation without the need for this XML mess:

public String submit() {
    // ...

    return "/foo.xhtml?faces-redirect=true&param=bar";
}

If you're inside an event or ajax listener method which can't return a string outcome, then you could always grab NavigationHandler#handleNavigation() to perform the desired navigation.

public void someEventListener(SomeEvent event) { // AjaxBehaviorEvent or ComponentSystemEvent or even just argumentless.
    // ...

    FacesContext context = FacesContext.getCurrentInstance();
    NavigationHandler navigationHandler = context.getApplication().getNavigationHandler();
    navigationHandler.handleNavigation(context, null, "from-outcome");
}

The non-navigation-case equivalent for that is using ExternalContext#redirect().

like image 70
BalusC Avatar answered Oct 30 '22 13:10

BalusC