我从>< 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侦听器方法中执行导航
发布评论