6 Replies Latest reply on Jun 11, 2009 5:36 AM by Anton Gavazuk

    An PhaseListener blocks richfaces css styles

    Anton Gavazuk Newbie

      Hi all,

      I'm using MyFaces 1.2 impl with appropriate Tomahawk library on JBoss 4.2.3 GA. Richfaces version is 3.3.1 GA (but the same situation could be reproduced with previous versions)

      I implemented such PhaseListener:

      public void afterPhase(PhaseEvent event) {
      
       FacesContext fc = event.getFacesContext();
       String view = fc.getViewRoot().getViewId();
       //is page is should be checked?
      
       boolean isCommonPage = view.indexOf("/common") != -1;
      
       if (!isCommonPage && getCurrentSystemUser(fc) == null) {
       debug("User is not logged - redirecting to login page");
       NavigationHandler nh = fc.getApplication().getNavigationHandler();
       //TODO add message in faces context!!!!
       nh.handleNavigation(fc, null, "login");
       }
       }
      
      public PhaseId getPhaseId() {
       return PhaseId.RESTORE_VIEW;
       }
      
      

      and switched on the phase listener in faces config.
      When the PhaseListener is ON, the stylesheets below are empty:

      /a4j/s/3_3_1.GAcss/panel.xcss/DATB/eAF7sqpgb-jyGdIAFrMEaw__.jsf
      /a4j/s/3_3_1.GAcss/separator.xcss/DATB/eAF7sqpgb-jyGdIAFrMEaw__.jsf
      /a4j/s/3_3_1.GAorg/richfaces/renderkit/html/css/extended_classes.xcss/DATB/eAF7sqpgb-jyGdIAFrMEaw__.jsf
      /a4j/s/3_3_1.GAorg/richfaces/renderkit/html/css/basic_classes.xcss/DATB/eAF7sqpgb-jyGdIAFrMEaw__.jsf


      When I'm switching off the PhaseListener everything is fine and works pretty well. Or even if I delete from the afterPhase method call to navigationHandle it works well.

      The PhaseListener doesnt intercept the requests for stylesheets above, so I dont know how its possible that call to navigationHandler affects CSS fetching.

      Part of my web.xml:
      <filter-mapping>
       <filter-name>richfaces</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
       <dispatcher>REQUEST</dispatcher>
       <dispatcher>FORWARD</dispatcher>
       <dispatcher>INCLUDE</dispatcher>
       </filter-mapping>
      
       <filter-mapping>
       <filter-name>extensionsFilter</filter-name>
       <url-pattern>*.jsf</url-pattern>
       </filter-mapping>
      


      Could anyone help me with the issue?

        • 1. Re: An PhaseListener blocks richfaces css styles
          Ilya Shaikovsky Master

          Non-redirect navigation not intended to works with ajax requests. Is it your case?

          • 2. Re: An PhaseListener blocks richfaces css styles
            Anton Gavazuk Newbie

            Hi Ilya,

            thanks for the answer

            Short case description:
            PhaseListener checks if a user accessing a page which requires the user personal data, and if its true, then the PhaseListener redirects request to certain predefined page.
            The solution works fine except that richfaces CSS styles are not applied to the components.

            I defined the rule for "login" outcome as:

            <navigation-case>
             <from-outcome>login</from-outcome>
             <to-view-id>/common/login.jsp</to-view-id>
             <redirect/>
             </navigation-case>
            

            So JSF controller must make redirect to a specified resource.

            Anyway, if even I would make this redirect manually by
            FacesContext.getCurrentInstance().getExternalContext().redirect("http://localhost:8080/hms/common/login.jsf");

            I wouldnot see the applied CSS styles.

            The strangest thing is that CSS styles is not applied even if user is logged in and redirect is not performed - I mean that code snippet
            if (!isCommonPage && getCurrentSystemUser(fc) == null) {
             debug("User is not logged - redirecting to login page");
             NavigationHandler nh = fc.getApplication().getNavigationHandler();
             //TODO add message in faces context!!!!
             nh.handleNavigation(fc, null, "login");
             }
            
            is not executed.

            How can I fix this? Apply another ways of session check? Redefine the default richfaces CSS styles? I'm wondered that noone has seen the same problem before...



            • 3. Re: An PhaseListener blocks richfaces css styles
              Nick Belaevski Master

               

              The PhaseListener doesnt intercept the requests for stylesheets above, so I dont know how its possible that call to navigationHandler affects CSS fetching.

              In fact, it does; isCommonPage = false for CSS resources. Use this: org.ajax4jsf.webapp.WebXml#getFacesResourceKey(HttpServletRequest) to determine whether the request is resource request or not.

              • 4. Re: An PhaseListener blocks richfaces css styles
                Anton Gavazuk Newbie

                 

                "nbelaevski" wrote:
                The PhaseListener doesnt intercept the requests for stylesheets above, so I dont know how its possible that call to navigationHandler affects CSS fetching.

                In fact, it does; isCommonPage = false for CSS resources. Use this: org.ajax4jsf.webapp.WebXml#getFacesResourceKey(HttpServletRequest) to determine whether the request is resource request or not.


                Hi Nick,

                I had performed the debug before - PhaseListener doesnt intercept the PhaseEvent for viewId "/a4j/s/....something" - thats why I was wondered how PhaseListener impacts the CSS fetching.

                Anyway - could you give me an example with org.ajax4jsf.webapp.WebXml#getFacesResourceKey usage?

                Zaranee spasibo :)

                • 5. Re: An PhaseListener blocks richfaces css styles
                  Nick Belaevski Master

                  If getFacesResourceKey() returns not null, then it's resource request. Use WebXml.getInstance() to obtain instance of WebXml class.

                  • 6. Re: An PhaseListener blocks richfaces css styles
                    Anton Gavazuk Newbie

                     

                    "nbelaevski" wrote:
                    If getFacesResourceKey() returns not null, then it's resource request. Use WebXml.getInstance() to obtain instance of WebXml class.


                    Hi Nick,

                    thanks - it works!

                    the final version would look like:
                    public void afterPhase(PhaseEvent event) {
                     debug("afterPhase started " + event.getPhaseId() + " source " + event.getSource());
                     FacesContext fc = event.getFacesContext();
                     String view = fc.getViewRoot().getViewId();
                     debug("afterPhase viewId=" + view);
                     //is page is should be checked?
                     boolean isCommonPage = view.indexOf("/common") != -1;
                     if (WebXml.getInstance(fc).getFacesResourceKey((HttpServletRequest) fc.getExternalContext().getRequest()) != null) {
                     return;
                     }
                     if (!isCommonPage && getCurrentSystemUser(fc) == null) {
                     debug("User is not logged - redirecting to login page");
                     NavigationHandler nh = fc.getApplication().getNavigationHandler();
                     //TODO add message in faces context!!!!
                     nh.handleNavigation(fc, null, "login");
                     } else {
                     debug("User is logged in - go to page "
                     + view);
                     }
                     debug("afterPhase finished " + event.getPhaseId() + " source " + event.getSource());
                     }