本文介绍了如何在 preRenderView 侦听器方法中执行导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从什么可以开始, <f:viewParam>和 <f:viewAction>用于?

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

I have a pre render view event listener:

<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>

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

However, it doesn't perform the navigation. It works when I use a command button as follows:

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

推荐答案

基于方法返回值的导航仅由实现 ActionSource2 接口并为此提供一个采用 MethodExpression 的属性,例如 <UICommand的code>action属性 组件,在Apply Request Values 阶段排队,在Invoke Application 阶段调用.

Navigation based on a method's return value is only performed by components implementing ActionSource2 interface and providing an attribute taking a MethodExpression for that, such as action attribute of UICommand components, which is queued during Apply Request Values phase and invoked during Invoke Application phase.

仅仅是一个 组件系统事件 侦听器方法,而不是操作方法.您需要手动执行如下导航:

The <f:event listener> is merely a component system event listener method, not an action method. You need to perform the navigation manually as follows:

public void performWeakLogin() {
    // ...

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

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

Alternatively, you can also send a redirect on a given URL, which is more useful for the case you don't want to navigate internally, but externally:

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

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


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


Unrelated to the concrete problem, a servlet filter is a better place for the job of performing request based authorization/authentication.

这篇关于如何在 preRenderView 侦听器方法中执行导航的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:42