3 Replies Latest reply on Nov 13, 2006 10:41 AM by Cyril JOUI

    Bug or not ? (request attributes lost)

    Cyril JOUI Newbie

      Hello,

      Following my last post : http://jboss.org/index.html?module=bb&op=viewtopic&t=90549
      I think I've found a problem with JSF / Portlet and request attribute.

      I made a simple application (add user / list user / edit user) with one managedBean (userBean) in request scope.

      The problem is that for 1 request, JSF create two instances of my managedBean (User).
      This is due to processAction and facesRender methods which takes 2 differents PortletRequest object in arguments.

      First JSF creates the userBean managed instance in request scope (in processAction method).

      Then, in facesRender method, when rendering the page JSF creates another instance of my userBean managed !

      The problem comes from this code line :

      facesContext.setExternalContext(makeExternalContext(request, response));
      


      The request object doesn't have request attributs from processAction method ...

      I wanted to know if it's normal or not ? And if not, how do you do with JSF to have one instance of a managed bean request scoped in one request ?

      Thanks by advance.

      Sorry for my poor english !

      Cyril


        • 1. Re: Bug or not ? (request attributes lost)
          Cyril JOUI Newbie

          I found some informations :
          http://issues.apache.org/jira/browse/MYFACES-788

          It seems to be a MyFaces bug ...

          Does anyone have an idea ?

          Thanks

          • 2. Re: Bug or not ? (request attributes lost)
            Jacob Boje Newbie

            Does anyone have a solution or answer to this problem? I think it's a big problem that you can't user request scope propertly when runing JSF in JBoss Portal

            • 3. Re: Bug or not ? (request attributes lost)
              Cyril JOUI Newbie

              Hello,

              I use a custom "faces-context-factory" in my faces-config.xml :

              <factory>
               <faces-context-factory>com.labosun.portal.myfaces.context.PortletFacesContextFactory</faces-context-factory>
               </factory>


              FacesContextImpl :

              package com.labosun.portal.myfaces.context;
              
              import java.util.Iterator;
              import java.util.Set;
              
              import javax.faces.context.ExternalContext;
              import javax.portlet.PortletContext;
              import javax.portlet.PortletRequest;
              import javax.portlet.PortletResponse;
              
              import org.apache.commons.logging.Log;
              import org.apache.commons.logging.LogFactory;
              import org.apache.myfaces.context.ReleaseableExternalContext;
              import org.apache.myfaces.context.servlet.ServletFacesContextImpl;
              
              public class PortletFacesContextImpl extends ServletFacesContextImpl {
              
               private final static Log logger = LogFactory
               .getLog(PortletFacesContextImpl.class);
              
               public PortletFacesContextImpl(PortletContext ctx, PortletRequest request,
               PortletResponse response) {
               super(ctx, request, response);
               }
              
               public void setExternalContext(ReleaseableExternalContext context) {
               ExternalContext extNew = (ExternalContext) context;
              
               // update new external context
               Set keySet = getExternalContext().getRequestMap().keySet();
               Iterator keySetIt = keySet.iterator();
               while (keySetIt.hasNext()) {
               String att = (String) keySetIt.next();
               if (extNew.getRequestMap().get(att) == null) {
               extNew.getRequestMap().put(att,
               getExternalContext().getRequestMap().get(att));
               }
               }
               super.setExternalContext(context);
               }
              }
              


              package com.labosun.portal.myfaces.context;
              
              import javax.faces.FacesException;
              import javax.faces.context.FacesContext;
              import javax.faces.context.FacesContextFactory;
              import javax.faces.lifecycle.Lifecycle;
              import javax.portlet.PortletContext;
              import javax.portlet.PortletRequest;
              import javax.portlet.PortletResponse;
              
              public class PortletFacesContextFactoryImpl extends FacesContextFactory {
              
               public FacesContext getFacesContext(Object context, Object request,
               Object response, Lifecycle lifecycle) throws FacesException {
              
               if (context == null) {
               throw new NullPointerException("context");
               }
               if (request == null) {
               throw new NullPointerException("request");
               }
               if (response == null) {
               throw new NullPointerException("response");
               }
               if (lifecycle == null) {
               throw new NullPointerException("lifecycle");
               }
              
               return new PortletFacesContextImpl((PortletContext) context,
               (PortletRequest) request, (PortletResponse) response);
               }
              
              }
              


              It works .it! but it may have some troubles if you have many portlets which uses MyFaces in one page ... (maybe ... I have no problem but I didn't test
              a lot !


              Good luck !