如何在 preRenderView 侦听器方法中执行导航

编程入门 行业动态 更新时间:2024-10-11 13:20:26
本文介绍了如何在 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 的属性,例如 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.

  • 有什么简单的方法可以预处理和重定向 GET 请求?

更多推荐

如何在 preRenderView 侦听器方法中执行导航

本文发布于:2023-11-24 19:38:11,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1626546.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:侦听器   方法   如何在   preRenderView

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!