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

编程入门 行业动态 更新时间:2024-10-11 15:24:07
本文介绍了如何在preRenderView侦听器方法中执行导航的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我从>< f:metadata> ,< 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 组件,该组件在 Apply期间排队请求值阶段,并在调用应用程序阶段被调用.

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.

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

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

发布评论

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

>www.elefans.com

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