如何使用导航规则进行重定向 [英] How to make redirect using navigation-rule

查看:82
本文介绍了如何使用导航规则进行重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以通过

externalContext.redirect("foo.xhtml");

但是我想使用在<navigation-rule/>(faces-config.xml)中为<from-outcome/>&&传递命令行参数.

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");

不起作用.该怎么做?

推荐答案

ExternalContext#redirect()

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

您需要从声明为返回String的操作方法中返回导航结果.

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

public String submit() {
    // ...

    return "from-outcome";
}

您可以通过添加<redirect>来配置导航案例以发送重定向.

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>

请注意,您也可以只使用JSF隐式导航,而无需使用这种XML混乱:

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";
}

如果您处于无法返回字符串结果的事件或ajax侦听器方法中,则可以始终抓住NavigationHandler#handleNavigation()来执行所需的导航.

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");
}

使用ExternalContext#redirect()的非导航用例.

这篇关于如何使用导航规则进行重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆