使用JSF PhaseListener重定向到页面

时间:2013-03-29 13:47:16

标签: jsf-2 liferay-6

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

场景2: 如果如果friendlyURL是'/ requestform / servicerequest'并且requestProcessorBean.userRequestVO != null然后重定向到'serviceRequest.xhtml'页面。

我想知道Scenario 1如何使用JSF Phase Listener实现。我已经实现了方案1,如下所示: 的 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。

由于

1 个答案:

答案 0 :(得分:0)

我真的不会推荐条件重定向的阶段监听器。请改用<f:event type="preRenderView"/>。但是,如果由于某种原因你被限制使用阶段隐藏器,你的实现将如下所示:

  1. 将PhaseListener定义为此

    /*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
       }
    }
    
  2. 在您想要的页面上使用下面的标记注册新的PhaseListener

    <f:phaseListener type="com.you.TestPhaseListener"/>
    
  3. 就像我在本文开头提到的那样,这种方法不必要地笨拙,太多的努力来实现这么少和IMO,滥用阶段助手。