Redirect to page using JSF PhaseListener

让人想犯罪 __ 提交于 2019-12-25 04:55:29

问题


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

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

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>

I am using Liferay 6.0 and JSF 2.0.

Thanks


回答1:


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. 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
       }
    }
    
  2. Register your new PhaseListener with the tag below on the page you desire

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

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.



来源:https://stackoverflow.com/questions/15704581/redirect-to-page-using-jsf-phaselistener

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!