gpt4 book ai didi

jsf-2 - 如何在 preRenderView 监听器方法中执行导航

转载 作者:行者123 更新时间:2023-12-02 06:19:31 25 4
gpt4 key购买 nike

我从 What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for? 开始

我有一个预渲染 View 事件监听器:

<f:metadata>
<f:event type="preRenderView" listener="#{loginBean.performWeakLogin()}" />
</f:metadata>

它调用以下方法:

public String performWeakLogin() {
FacesContext facesContext = FacesContext.getCurrentInstance();
String parameter_value = (String) facesContext.getExternalContext().getRequestParameterMap().get("txtName");

if (parameter_value != null && parameter_value.equalsIgnoreCase("pippo")) {
try {
return "mainPortal";
} catch (IOException ex) {
return null;
}
} else {
return null;
}
}

以及以下导航规则:

<navigation-rule>
<from-view-id>/stdPortal/index.xhtml</from-view-id>
<navigation-case>
<from-outcome>mainPortal</from-outcome>
<to-view-id>/stdPortal/stdPages/mainPortal.xhtml</to-view-id>
<redirect/>
</navigation-case>
</navigation-rule>

但是,它不执行导航。当我使用命令按钮时它会起作用,如下所示:

<p:commandButton ... action="#{loginBean.performWeakLogin()}"  /> 

最佳答案

基于方法返回值的导航仅由实现 ActionSource2 的组件执行接口(interface)并提供一个采用 MethodExpression 的属性为此,例如 action UICommand 的属性组件,在应用请求值阶段排队,并在调用应用程序阶段调用。

<f:event listener>只是一个component system event监听器方法,而不是操作方法。您需要手动执行导航,如下所示:

public void performWeakLogin() {
// ...

FacesContext fc = FacesContext.getCurrentInstance();
fc.getApplication().getNavigationHandler().handleNavigation(fc, null, "mainPortal");
}

或者,您也可以在给定 URL 上发送重定向,这对于您不想在内部导航而是在外部导航的情况更有用:

public void performWeakLogin() throws IOException {
// ...

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/stdPortal/stdPages/mainPortal.xhtml");
}
<小时/>

与具体问题无关servlet filter是执行基于请求的授权/身份验证工作的更好地方。

另请参阅:

关于jsf-2 - 如何在 preRenderView 监听器方法中执行导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16106418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com