struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Néstor Boscán <>
Subject RE: AJAX with DWR but without DOJO?
Date Fri, 13 Feb 2009 19:02:29 GMT
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,

      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">
            <param name="class"
        <convert converter="bean"
            <param name="include"
        <convert converter="bean" match="MyAction">
            <param name="include"
                   value="fieldErrors, actionErrors "/>

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.


Néstor Boscán

-----Mensaje original-----
De: lllewell [] 
Enviado el: Friday, February 13, 2009 12:13 PM
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: .  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!

View this message in context:
Sent from the Struts - User mailing list archive at

To unsubscribe, e-mail:
For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message