jsf - 如何使用导航规则进行重定向

标签 jsf redirect jsf-2 navigation

在JSF中从Bean重定向可以通过以下方式完成:

externalContext.redirect("foo.xhtml");

但是我想使用在<navigation-rule/>(faces-config.xml)中设置的规则为<from-outcome/> &&传递命令行参数。
externalContext.redirect("from-outcome?param=bar");

不管用。怎么做?

最佳答案

ExternalContext#redirect() takes an URL,而不是导航结果。

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

public String submit() {
    // ...

    return "from-outcome";
}

您可以通过添加<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困惑:
public String submit() {
    // ...

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

如果您处于无法返回字符串结果的事件或ajax监听器方法中,则可以始终获取NavigationHandler#handleNavigation()来执行所需的导航。
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()

关于jsf - 如何使用导航规则进行重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18512547/

相关文章:

java - Primefaces 数据表不起作用

java - 似乎无法弄清楚如何使用 primefaces 重定向到 .xhtml

子shell进程双向重定向到父进程

apache - 只对几个 URL 强制使用 HTTPS?

jsf - 使用 primefaces 进行部分处理时,数据网格的最后一个条目丢失

javascript - PrimeFaces 5.0 javascript 错误 - b.debug("PrimeFaces 已经加载

javascript - 使用正则表达式进行电子邮件验证不适用于 jsf?

jsf - 更改 Bootsfaces 数据表的区域设置和语言

redirect - 临时重定向以避免重复内容

jsf - 所需的验证取决于特定按钮在向导中不起作用