在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¶m=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/