5 Replies Latest reply on Sep 30, 2010 12:42 PM by Leonardo Nascimento

    FYI: Seam and DWR do work together

    Ronald van Kuijk Apprentice

      I've been looking into using seam and DWR together and got it to work. In the end it was not that difficult, just one big puzzle.


      It turns out that DWR can access components from the JSF tree without the request actually being part of the JSF phases. To achieve this add the following (in addition to the dwr servlet) in your web.xml:


      <filter>
          <filter-name>DwrFacesFilter</filter-name>
          <filter-class>org.directwebremoting.faces.FacesExtensionFilter</filter-class>
      </filter
      
      <filter-mapping>
          <filter-name>DwrFacesFilter</filter-name>
          <url-pattern>/dwr/*</url-pattern>
      </filter-mapping>



      and


      <web:context-filter url-pattern="/dwr/*"/>



      to your components.xml


      Configure your 'seam' components like this in dwr.xml:


      <allow>
          <create creator="jsf" javascript="myDWRJS" scope="request">
              <param name="class" value="tld.domain.package.Component"/>
              <param name="managedBeanName" value="my.component.name"/>
          </create>
      </allow>




      In DWR they say it is good practice to have a 'facade' for your real class. This facade is the one you configure in the dwr.xml and in it. you can @In whatever you want from whatever scope. I gave the facade itself a 'stateless' scope.


      Now the tricky part is that DWR does now about the conversation-id parameter. You can set any parameter in your corresponding dwr javascript file via


      dwr.engine.setParameters({"cid": myCid}); 
      


      But you have to retrieve it from somewhere. In my case 'myCid' is a hidden field in an xform page and retrieved via dom.


      but unfortunately they are never really set on the request url. Use the following patch for that


      Index: java/org/directwebremoting/engine.js
      ===================================================================
      --- java/org/directwebremoting/engine.js     (revision 2561)
      +++ java/org/directwebremoting/engine.js     (working copy)
      @@ -765,6 +765,18 @@
               request.url += encodeURIComponent(prop) + "=" + encodeURIComponent(batch.map[prop]) + "&";
             }
           }
      +    
      +    try {
      +      for (prop in batch.parameters) {
      +        var value = batch.parameters[prop];
      +        if (typeof value == "string") {
      +          request.url += encodeURIComponent(prop) + "=" + encodeURIComponent(batch.parameters[prop]) + "&";
      +        }
      +      }
      +    }
      +    catch (ex) {
      +      dwr.engine._handleWarning(batch, ex);
      +    }
           request.url = request.url.substring(0, request.url.length - 1);
         }
         else {
      @@ -788,8 +800,24 @@
               }
             }
           }
      +    
      +    request.url += "?";
      +    try {
      +      for (prop in batch.parameters) {
      +        var value = batch.parameters[prop];
      +        if (typeof value == "string") {
      +          request.url += encodeURIComponent(prop) + "=" + encodeURIComponent(batch.parameters[prop]) + "&";
      +        }
      +      }
      +    }
      +    catch (ex) {
      +      dwr.engine._handleWarning(batch, ex);
      +    }
      +    request.url = request.url.substring(0, request.url.length - 1);
      +
           request.body = dwr.engine._contentRewriteHandler(request.body);
         }
      +  
         request.url = dwr.engine._urlRewriteHandler(request.url);
         return request;
       };
      



      Et voila....


      And all the best for 2009...