jsf 1.2 在恢复视图阶段获取错误的 viewID

发布于 2024-12-12 00:45:27 字数 5461 浏览 0 评论 0原文

在我的项目中,我使用以下框架:sun jsf 1.2、spring 2.5、RichFaces ajax4faces、apache Orchestra 和 Facelets。

我开发了自己的 PhaseListener 实现来捕获传入页面并通过 Java 接口处理 init preproces 和 prerender 事件。

当我从 HTML 页面上的链接调用页面并调用 afterPhase 事件时,我在恢复视图阶段获得正确的 viewid,但是当我在调用恢复视图阶段时使用 commandButton(使用 navigation.xml 配置文件)调用另一个页面时viewid 仍然与调用者页面相同,但与调用者页面不同,我得到调用页面的正确 viewid,直到调用 invoke_application 阶段。 因此,使用commnadButton调用时,调用页面的init()方法不会运行。

here my phaselistener implementation program

package hsbpbnp;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class AppPhaseListener implements PhaseListener {

        PhaseId phaseid           = null ;
        FacesContext facesContext = null ;

//constructor de la clase
    public AppPhaseListener() {
    }

     @Override
     public void beforePhase( PhaseEvent phaseEvent ) {

         phaseid = phaseEvent.getPhaseId();

         if ( phaseid == PhaseId.RENDER_RESPONSE ) {

            facesContext  = phaseEvent.getFacesContext() ;
            String viewId = phaseEvent.getFacesContext().getViewRoot().getViewId() ;
         if( viewId.endsWith( ".xhtml" ) ) {
             String ManagedBeanName = this.getFileName( viewId ) ;
            Object object           = (Object) FacesUtils.getManagedBean( ManagedBeanName ) ;
          if ( object == null ) {
              System.out.println( "no such managed bean " + viewId ) ;
          }else {
              ILifeCycleAware lifeCycleAwareBean = ( ILifeCycleAware ) object ;
              lifeCycleAwareBean.prerender() ;
          }
         }
        }//Fin if RENDER_RESPONSE

    }//Fin del metodo beforePhase

     @Override
     public void afterPhase( PhaseEvent phaseEvent ) {
         phaseid                = phaseEvent.getPhaseId() ;
         SessionBean1 sb        = ( SessionBean1 )FacesUtils.getManagedBean( "SessionBean1" );
         String ManagedBeanName = " " ;
         if ( phaseid == PhaseId.RESTORE_VIEW ) {


            facesContext        = phaseEvent.getFacesContext();
            String viewId       = phaseEvent.getFacesContext().getViewRoot().getViewId();

         if ( viewId.indexOf( "css" ) != -1 ) return;

         if( viewId.endsWith( ".xhtml" ) ) {
            ManagedBeanName = this.getFileName( viewId ) ;
            Object object   = ( Object ) FacesUtils.getManagedBean( ManagedBeanName ) ;
          if ( object == null ) {
              System.out.println( "no such managed bean " + viewId ) ;
          }else {
              ILifeCycleAware lifeCycleAwareBean = ( ILifeCycleAware ) object ;
              lifeCycleAwareBean.init() ;
          }
         }

          String ManagedBean = this.getManagedName( viewId );

        if( sb.getLastPage().equals( ManagedBean ) ) {
            try {
                if(FacesUtils.getRequestParameter("from").equals("menu")){
                    sb.setPostback(false);
                    return;
                }
            } catch (Exception ex){
               //do nothing 
            }

             sb.setPostback( true );

         }else {



           String pop = this.firstFive( ManagedBean );

           if( pop.equals( "Popup" ) ) {

               return;


           }else {

               sb.setLastPage( ManagedBean );
               sb.setPostback( false );
               sb.setOnLoad1 ( " " );
           }

         }

       }//Fin if RESTORE_VIEW

        if( phaseid == PhaseId.INVOKE_APPLICATION ) {
            facesContext  = phaseEvent.getFacesContext() ;
            String viewId = phaseEvent.getFacesContext().getViewRoot().getViewId() ;
         if( viewId.endsWith( ".xhtml" ) ) {
             ManagedBeanName = this.getFileName( viewId ) ;
            Object object           = ( Object ) FacesUtils.getManagedBean( ManagedBeanName ) ;
          if ( object == null ) {
              System.out.println( "no such managed bean " + viewId ) ;
          }else {
              ILifeCycleAware lifeCycleAwareBean = ( ILifeCycleAware ) object ;
              lifeCycleAwareBean.preprocess() ;
          }
         }
        }//Fin if INVOKE_APPLICATION

    }//Fin del metodo afterPhase

     @Override
     public PhaseId getPhaseId() {
         return PhaseId.ANY_PHASE ;
     }//Fin del metodo getPhaseId

//Metodo que se encarga de encontrar el nombre del bean de la
//pagina que esta siendo creada en el ciclo de vida

 public String getManagedName( String path ) {

        String fileName  = null;
        String separator = "/";
        int pos          = path.lastIndexOf( separator );
        int pos2         = path.lastIndexOf( "." );
        if( pos2 >- 1 )
            fileName     = path.substring( pos + 1, pos2 );
        else
            fileName     = path.substring( pos + 1 );
        return fileName;

  }//Fin del metodo getFileName

     public String getFileName( String path ) {

        String fileName  = null;
        String separator = "/";
        int sep          = path.lastIndexOf( separator );
        int dot          = path.lastIndexOf( "." );
        fileName         = path.substring( sep + 1, dot );

    return fileName.substring( 0,1 ).toLowerCase() + fileName.substring( 1 );

  }//Fin del metodo getFileName

   public String firstFive( String managedBean ) {

      String pop = managedBean.substring(0, 5);

      return pop;

  }//fin del metodo firstFive

}//EOF

In my project that I am using the following framework: sun jsf 1.2, spring 2.5, RichFaces ajax4faces, apache orchestra and Facelets.

I developed my own PhaseListener Implementation to trap incoming pages and handling init preproces and prerender events through a Java interface.

When I call a page from a link on an HTML page and afterPhase event is called , I get the correct viewid in the restore_view phase, but when I call another page using a commandButton (using navigation.xml config file) when restore_view phase is invoke the viewid still the same of caller page but not the calling one, i get the correct viewid of calling page until invoke_application phase is invoke.
Therefore the init () method of calling pages do not run when using commnadButton to call it.

here my phaselistener implementation program

package hsbpbnp;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;

public class AppPhaseListener implements PhaseListener {

        PhaseId phaseid           = null ;
        FacesContext facesContext = null ;

//constructor de la clase
    public AppPhaseListener() {
    }

     @Override
     public void beforePhase( PhaseEvent phaseEvent ) {

         phaseid = phaseEvent.getPhaseId();

         if ( phaseid == PhaseId.RENDER_RESPONSE ) {

            facesContext  = phaseEvent.getFacesContext() ;
            String viewId = phaseEvent.getFacesContext().getViewRoot().getViewId() ;
         if( viewId.endsWith( ".xhtml" ) ) {
             String ManagedBeanName = this.getFileName( viewId ) ;
            Object object           = (Object) FacesUtils.getManagedBean( ManagedBeanName ) ;
          if ( object == null ) {
              System.out.println( "no such managed bean " + viewId ) ;
          }else {
              ILifeCycleAware lifeCycleAwareBean = ( ILifeCycleAware ) object ;
              lifeCycleAwareBean.prerender() ;
          }
         }
        }//Fin if RENDER_RESPONSE

    }//Fin del metodo beforePhase

     @Override
     public void afterPhase( PhaseEvent phaseEvent ) {
         phaseid                = phaseEvent.getPhaseId() ;
         SessionBean1 sb        = ( SessionBean1 )FacesUtils.getManagedBean( "SessionBean1" );
         String ManagedBeanName = " " ;
         if ( phaseid == PhaseId.RESTORE_VIEW ) {


            facesContext        = phaseEvent.getFacesContext();
            String viewId       = phaseEvent.getFacesContext().getViewRoot().getViewId();

         if ( viewId.indexOf( "css" ) != -1 ) return;

         if( viewId.endsWith( ".xhtml" ) ) {
            ManagedBeanName = this.getFileName( viewId ) ;
            Object object   = ( Object ) FacesUtils.getManagedBean( ManagedBeanName ) ;
          if ( object == null ) {
              System.out.println( "no such managed bean " + viewId ) ;
          }else {
              ILifeCycleAware lifeCycleAwareBean = ( ILifeCycleAware ) object ;
              lifeCycleAwareBean.init() ;
          }
         }

          String ManagedBean = this.getManagedName( viewId );

        if( sb.getLastPage().equals( ManagedBean ) ) {
            try {
                if(FacesUtils.getRequestParameter("from").equals("menu")){
                    sb.setPostback(false);
                    return;
                }
            } catch (Exception ex){
               //do nothing 
            }

             sb.setPostback( true );

         }else {



           String pop = this.firstFive( ManagedBean );

           if( pop.equals( "Popup" ) ) {

               return;


           }else {

               sb.setLastPage( ManagedBean );
               sb.setPostback( false );
               sb.setOnLoad1 ( " " );
           }

         }

       }//Fin if RESTORE_VIEW

        if( phaseid == PhaseId.INVOKE_APPLICATION ) {
            facesContext  = phaseEvent.getFacesContext() ;
            String viewId = phaseEvent.getFacesContext().getViewRoot().getViewId() ;
         if( viewId.endsWith( ".xhtml" ) ) {
             ManagedBeanName = this.getFileName( viewId ) ;
            Object object           = ( Object ) FacesUtils.getManagedBean( ManagedBeanName ) ;
          if ( object == null ) {
              System.out.println( "no such managed bean " + viewId ) ;
          }else {
              ILifeCycleAware lifeCycleAwareBean = ( ILifeCycleAware ) object ;
              lifeCycleAwareBean.preprocess() ;
          }
         }
        }//Fin if INVOKE_APPLICATION

    }//Fin del metodo afterPhase

     @Override
     public PhaseId getPhaseId() {
         return PhaseId.ANY_PHASE ;
     }//Fin del metodo getPhaseId

//Metodo que se encarga de encontrar el nombre del bean de la
//pagina que esta siendo creada en el ciclo de vida

 public String getManagedName( String path ) {

        String fileName  = null;
        String separator = "/";
        int pos          = path.lastIndexOf( separator );
        int pos2         = path.lastIndexOf( "." );
        if( pos2 >- 1 )
            fileName     = path.substring( pos + 1, pos2 );
        else
            fileName     = path.substring( pos + 1 );
        return fileName;

  }//Fin del metodo getFileName

     public String getFileName( String path ) {

        String fileName  = null;
        String separator = "/";
        int sep          = path.lastIndexOf( separator );
        int dot          = path.lastIndexOf( "." );
        fileName         = path.substring( sep + 1, dot );

    return fileName.substring( 0,1 ).toLowerCase() + fileName.substring( 1 );

  }//Fin del metodo getFileName

   public String firstFive( String managedBean ) {

      String pop = managedBean.substring(0, 5);

      return pop;

  }//fin del metodo firstFive

}//EOF

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文