2013-03-29 65 views
0

方案1: 如果friendlyURL是 '/ requestform/servicerequest' 和requestProcessorBean.userRequestVO == null然後驗證會話,並重定向到「/網絡/ PDS /登錄頁面..重定向頁面使用JSF PhaseListener在

方案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。

感謝

+0

你是積極的,你想要的是重定向?你現在正在做的是動態包含,而不是重定向(這實際上導致用戶被帶到另一個視圖)。請指定您真正需要的內容 – kolossus

+0

我想爲方案1重定向我想重定向。 – Sri

回答

0

我真的不會推薦一個階段偵聽條件重定向。改爲使用<f:event type="preRenderView"/>。但是,如果由於某種原因,你限制使用的PhaseListener,您的實現應該是這樣的:

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

就像我在beginni提到在這篇文章中,這種方法不必要的笨重,太多的努力來實現這麼少的IMO,濫用phaselisteners。