使用JSF PhaseListener重定向到页面 [英] Redirect to page using JSF PhaseListener

查看:152
本文介绍了使用JSF PhaseListener重定向到页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

场景1: 如果friendlyURL是'/requestform/servicerequest'和requestProcessorBean.userRequestVO == null,则使会话无效并重定向到'/web/pds/login'页面.

Scenario 1: If the friendlyURL is '/requestform/servicerequest' and requestProcessorBean.userRequestVO == null then invalidate the session and redirect to '/web/pds/login' page..

方案2: 如果如果friendlyURL为'/requestform/servicerequest'并且requestProcessorBean.userRequestVO != null,则重定向到'serviceRequest.xhtml'页面.

Scenario 2: If the If the friendlyURL is '/requestform/servicerequest' and requestProcessorBean.userRequestVO != null then redirect to 'serviceRequest.xhtml' page.

我想知道如何使用JSF Phase Listener实现方案1.我已经实现了方案1,如下所示: requestForm.xhtml:

I want to know how I can Scenario 1 implement using JSF Phase Listener. I have implemented Scenario 1 as follows: requestForm.xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<f:view xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html" xmlns:ui="http://java.sun.com/jsf/facelets">
    <h:head>
    </h:head>
    <h:body>
        <h:outputFormat rendered="#{lookupBean.friendlyURL == '/requestform/servicerequest' and (requestProcessorBean.userRequestVO != null)}">
            <ui:include src="serviceRequest.xhtml" />
        </h:outputFormat>
    </h:body>
</f:view>

我正在使用Liferay 6.0和JSF 2.0.

I am using Liferay 6.0 and JSF 2.0.

谢谢

推荐答案

我真的不建议为条件重定向使用阶段侦听器.改为使用 <f:event type="preRenderView"/> .但是,如果由于某种原因而不得不使用相位侦听器,则您的实现应如下所示:

I really won't recommend a phase listener for the conditional redirect. Use instead, the <f:event type="preRenderView"/>. If however, for some reason you're constrained to use a phaselistener, your implementation would look like this:

  1. 定义一个PhaseListener看起来像这样

  1. Define a PhaseListener to look like this

/*we're defining the PhaseListener as a ManagedBean so
 * we can inject other beans into it 
 */
@ManagedBean(name = "thePhaseListener")
@RequestScoped
public class TestPhaseListener implements PhaseListener {


   @ManagedProperty(value = "#{requestProcessorBean}")
   transient private RequestProcessorBean requestProcessorBean;

   @ManagedProperty(value = "#{lookupBean}")
   transient private LookupBean lookupBean;

   @Override
   public void afterPhase(PhaseEvent event) {
      //throw new UnsupportedOperationException("Not supported yet.");
   }

   @Override
   public void beforePhase(PhaseEvent event) {

       try {

          if (lookupBean.getFriendlyURL.equals("/requestform/servicerequest") && (requestProcessorBean.getUserRequestVO() == null)) {
            event.getFacesContext().getExternalContext().redirect("/web/pds/login");
           }
        } catch (IOException ex) {
          Logger.getLogger(TestPhaseListener.class.getName()).log(Level.SEVERE, null, ex);
        }
     }

   @Override
   public PhaseId getPhaseId() {
      return PhaseId.RESTORE_VIEW; // The RESTORE_VIEW phase is the first in the lifecycle of a JSF view
   }
}

  • 在所需页面上的下面的标签中注册新的PhaseListener

  • Register your new PhaseListener with the tag below on the page you desire

    <f:phaseListener type="com.you.TestPhaseListener"/>
    

  • 就像我在本文开头提到的那样,这种方法不必要地笨拙,为实现如此之少和IMO而付出的太多努力.

    Like I mentioned at the beginning of this post, this approach is unnecessarily clunky, too much effort to achieve so little and IMO, an abuse of phaselisteners.

    这篇关于使用JSF PhaseListener重定向到页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆