0

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

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

我想知道如何使用 JSF Phase Listener 实现场景 1。我已经按如下方式实现了场景 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。

谢谢

4

1 回答 1

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"/>
    

就像我在这篇文章开头提到的那样,这种方法不必要地笨重,需要付出太多努力才能实现如此之少和 IMO,这是对相位监听器的滥用。

于 2013-03-31T04:34:35.750 回答