Hi Nestor - this works really well.  I still have work to do but you've got
me heading in the right direction.  Thanks so much for taking the time to
post!

I had one minor problem that took me a while to figure out, so I thought I'd
let you know in case you ever upgrade to DWR 2 or later.  Somewhere along
the line DWR enhanced the default security so that classes in package
'org.directwebremoting' (plus any method that take these classes as
arguments) can't be exposed to the outside world.  The symptom I observed
was that the generated DWRAction.js didn't include an execute() method, so
calls to it obviously failed.  

Because the DWRAction.execute method takes a
org.directwebremoting.webwork.ActionDefinition, the execute() method didn't
get added to the corresponding .js file.  I created an ActionDefinition in
my own codebase and everything worked.

Laura


nestorjb wrote:
> 
> The only thing that you need to configure with Struts 2 is to apply the
> Struts 2 Filter to the /dwr/* URL. On the JSP I also use a modified
> version of the WebWorks script so your JSP looks like this:
> 
> <script type="text/javascript" src="dwr/interface/DWRAction.js"></script>
> <script type="text/javascript" src="dwr/engine.js"></script>
> <script type="text/javascript" src="dwr/util.js"></script>
> <script type="text/javascript" src="js/DWRActionUtil.js"></script>
> 
>     DWRActionUtil.execute ({ action: 'action',
>                              method: 'save',
>                              namespace: '/namespace' },
>                            { clientId: 1024 },
>                            'formId',
>                            {
>                                callback: function (action) {
>                                    for (index = 0; index <
> action.clientList.length: index++) {
>                                       ...
>                                    }
>                                },
>                                errorHandler: function (message, exception)
> {
>                                }
>                            }
> 
> So the invocation passes the Action Definition object. Then you can pass
> the parameters as a JavaScript object or null, or you can pass the form Id
> so it can pass all parameters associated with a form. This works exactly
> like a form submit. Now if you want to return data, you return it as part
> of the Action attributes like you normally would (you have to declare this
> attributes in dwr.xml). 
> 
> DWRActionUtil.js:
> 
> if (dwr == null) var dwr = {};
> if (dwr.webwork == null) dwr.webwork = {};
> if (DWRActionUtil == null) var DWRActionUtil = dwr.webwork;
> 
> /** Execute a remote request using DWR */
> dwr.webwork.execute = function (action, values, forms, callbackObjOrName)
> {
>     var index1, index2, form, element;
> 
>     params = new Object();
> 
>     if (forms != null)
>     {
>         if (typeof forms == 'string') forms = [ forms ];
> 
>         for (index1 = 0; index1 < forms.length; index1++)
>         {
>             form = document.getElementById(forms [index1]);
>             for (index2 = 0; index2 < form.elements.length; index2++)
>             {
>                 element = form.elements [index2];
>                 params[element.name] = dwr.util.getValue(element);
>             }
>         }
>     }
> 
>     if (values != null)
>     {
>         for (element in values)
>             params[element] = values[element];
>     }
> 
>     // prepare action invocation object
>     var actionObj = {};
>     if (typeof action == 'string') {
>         var lastIdx = action.lastIndexOf('/');
>         actionObj.executeResult = 'true';
>         if (lastIdx != -1) {
>             actionObj.namespace = action.substring(0, lastIdx);
>             actionObj.action = action.substring(lastIdx + 1);
>         }
>         else {
>             actionObj.namespace = '';
>             actionObj.action = action;
>         }
>     }
>     else {
>         actionObj = action;
>     }
> 
>     // prepare the DWR callback object
>     var callbackObj = {};
>     var mustCall = false;
>     if (typeof callbackObjOrName == 'string') {
>         callbackObj.callback = function(dt) {
>             dwr.webwork.callback(dt, eval(callbackObjOrName));
>         };
>         mustCall = true;
>     }
>     else if (typeof callbackObjOrName == 'function') {
>         callbackObj.callback = function(dt) {
>             dwr.webwork.callback(dt, callbackObjOrName);
>         };
>         mustCall = true;
>     }
>     else if (typeof callbackObjOrName == 'object' && typeof
> callbackObjOrName.callback == 'function') {
>         for (var prop in callbackObjOrName) {
>             callbackObj[prop] = callbackObjOrName[prop];
>         }
>         callbackObj.callback = function(dt) {
>             dwr.webwork.callback(dt, callbackObjOrName.callback);
>         };
>         mustCall = true;
>     }
>     if (mustCall) {
>         DWRAction.execute(actionObj, params, callbackObj);
>     }
> };
> 
> /** Execute a remote request using DWR */
> dwr.webwork.callback = function(dt, originalCallback) {
>     if (dt.data) originalCallback(dt.data);
>     else if (dt.text) originalCallback(dt.text);
>     else originalCallback(dt);
> };
> 
> /** Utility to check to see if the passed object is an input element /
> element id */
> dwr.webwork.isElement = function(elementOrId) {
>     if (typeof elementOrId == "string") {
>         return true;
>     }
>     if (elementOrId.nodeName) {
>         var name = elementOrId.nodeName.toLowerCase();
>         if (name == 'input' || name == 'form') {
>             return true;
>         }
>     }
> 
>     return false;
> };
> 
> Regards,
> 
> Néstor Boscán
> 
> -----Mensaje original-----
> De: lllewell [mailto:laura.llewel...@gfs.com] 
> Enviado el: Friday, February 13, 2009 4:54 PM
> Para: user@struts.apache.org
> Asunto: RE: AJAX with DWR but without DOJO?
> 
> 
> Thanks Nestor, this looks interesting.  What would the JSP that used this
> action look like?  Is any special config required in the struts.xml?
> 
> 
> 
> nestorjb wrote:
>> 
>> I basically created a DWRAction class based on the one that WebWorks used
>> and I use it extensively on my Struts 2 applications:
>> 
>> import com.opensymphony.xwork2.ActionProxy;
>> 
>> import com.opensymphony.xwork2.ActionProxyFactory;
>>  
>> import java.util.Map;
>> import javax.servlet.ServletContext;
>> import javax.servlet.http.HttpServletRequest;
>> import javax.servlet.http.HttpServletResponse;
>> import org.apache.log4j.Logger;
>> import org.apache.struts2.dispatcher.Dispatcher;
>> import org.apache.struts2.dispatcher.mapper.ActionMapping;
>> import org.directwebremoting.webwork.ActionDefinition;
>> 
>> public class DWRAction {
>>   private static final Logger log = Logger.getLogger (DWRAction.class);
>>     
>>   public static Object execute (ActionDefinition actionDefinition, Map
>> params, HttpServletRequest request, HttpServletResponse response,
>> ServletContext servletContext) throws Exception {
>>     ActionMapping             mapping;
>>     Dispatcher                dispatcher;
>>     Map                       context;
>>     ActionProxy               proxy;
>> 
>>     try {
>>       dispatcher = Dispatcher.getInstance ();
>>       mapping    = new ActionMapping (actionDefinition.getAction(),
>> actionDefinition.getNamespace(), actionDefinition.getMethod(), params);
>>       context    = dispatcher.createContextMap (request, response,
>> mapping,
>> servletContext);
>> 
>>       proxy      = ((ActionProxyFactory) dispatcher.getContainer
>> ().getInstance (ActionProxyFactory.class)).createActionProxy
>> (actionDefinition.getNamespace (), actionDefinition.getAction (),
>> context,
>> false, false);
>>       proxy.setMethod (actionDefinition.getMethod ());
>>       proxy.execute   ();
>> 
>>       return (proxy.getAction ());
>>     }
>>     catch (Exception e) {
>>       log.error ("Cannot invoke action '" + actionDefinition.getAction()
>> +
>> "' in namespace '" + actionDefinition.getNamespace() + "'", e);
>>       throw e;
>>     }
>>   }
>> }
>> 
>> You must configure this class and ActionDefinition in you dwr.xml file
>> and
>> any Actions you wish to invoke
>> 
>>         <create creator="none" javascript="DWRAction">
>>             
>>         </create>
>>         <convert converter="bean"
>> match="org.directwebremoting.webwork.ActionDefinition">
>>             
>>         </convert>
>>         <convert converter="bean" match="MyAction">
>>             
>>         </convert>
>> 
>> Also is important that the Struts 2 Filter applies to the URL /dwr/*.
>> 
>> So with this code you can invoke any Action in your application, and
>> return
>> information from that Action. The validation framework will work here
>> too,
>> returning any validation errors in the fieldErrors attribute before
>> reaching
>> the action method. You can put validations in the validation framework or
>> you're validation method, or you can do AJAX invocations to retrieve
>> data.
>> 
>> Regards,
>> 
>> Néstor Boscán
>> 
>> -----Mensaje original-----
>> De: lllewell [mailto:laura.llewel...@gfs.com] 
>> Enviado el: Friday, February 13, 2009 12:13 PM
>> Para: user@struts.apache.org
>> Asunto: AJAX with DWR but without DOJO?
>> 
>> 
>> Greetings all -
>> 
>> I inherited a struts2 webapp that is using the "simple" theme all the way
>> through.  I'd like to add some Ajax validation to one of the pages using
>> DWR.  I'm interested in doing this without DOJO (theme="ajax").  
>> 
>> I see an example of how to do Prototype validation without DOJO here:
>> http://struts.apache.org/2.1.6/docs/ajax-validation.html .  I can use
>> that
>> as a basis, but I'm wondering if anyone has done something similar using
>> DWR
>> that you would be willing to share, or links to other reference materials
>> that would help me out.  I have googled this extensively and haven't had
>> much luck.
>> 
>> I'm planning to use Struts 2.1.6 and DWR 2.0.x (since DWR 3 isn't ready
>> for
>> prime time yet).
>> 
>> Thanks in advance!
>> Laura
>> 
>> 
>> -- 
>> View this message in context:
>> http://www.nabble.com/AJAX-with-DWR-but-without-DOJO--tp22000168p22000168.ht
>> ml
>> Sent from the Struts - User mailing list archive at Nabble.com.
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
>> For additional commands, e-mail: user-h...@struts.apache.org
>> 
>> 
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
>> For additional commands, e-mail: user-h...@struts.apache.org
>> 
>> 
>> 
> 
> -- 
> View this message in context:
> http://www.nabble.com/AJAX-with-DWR-but-without-DOJO--tp22000168p22005041.html
> Sent from the Struts - User mailing list archive at Nabble.com.
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
> For additional commands, e-mail: user-h...@struts.apache.org
> 
> 
> 

-- 
View this message in context: 
http://www.nabble.com/AJAX-with-DWR-but-without-DOJO--tp22000168p22059254.html
Sent from the Struts - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to