Sam Newman's site, a Consultant at ThoughtWorks

I’ve been playing around with “XWork”: a little today, attempting to work out how it all hangs together. I thought I’d share a simple XWork example I worked up as it should give you a simple introduction to the internals of XWork.


We want to invoke a bit of code and return a result. We would like to provide transparent logging.

The Action

First, we define the action

public class ExampleAction implements Action {
    public String execute() {
        return "Done";

The execute method will get invoked, and will return a value. We have to extend Action which I do not like, and we also throw exception form our exectue method which I dislike hugely, but understand the reasons for both.

The configuration

We define a config which declares our action, and defines the inteceptors we will use. “Interceptors”: are objects that get invoked before and after the action does. We are going to use two standard interceptors, one to log the action, the other to time it. We define an InterceptorStack which we can reuse – an InterceptorStack is simply a set of Interceptors that get invoked.

Now define our action and configure the use of the debugStack

Invoke the action

Very simple this:

try {
  ActionProxy proxy = factory.createActionProxy("default","TestAction", null);
  String result = proxy.execute();
  System.out.println("Calling action returns " + result);
} catch (Exception e) {

For the future

I’m going to look at creating a Swing framework that uses XWork. I can already feel an UndoRegistrationInterceptor coming on…

[Updated 18:45 22-02-2004]: Jason Carreira pointed out that I don’t need to create an ActionInvocation from my proxy, I can exectue the Proxy directly. The code has been changed accordingly.

[Updated 19:31 22-02-2004]: Jason Carreira pointed out other bonehead mistakes – updated 🙂

[Updated 19:31 22-02-2004]: Code fix – thanks to Brian Buckley.

10 Responses to “A Simple XWork example”

  1. Kris Thompson

    Very nice example! Nice to see an XWork example outside of the WebWork wrapper! Great work!

  2. Juan

    Thanks man, at OS they seem to have forgotten the value of ezamples, samples, tutorials and there is no implementation instructions on XWork which was why i didn’t even bother with it. Maybe your example will help us out.

  3. Sam Newman


    I feel your pain – I had to look at the sourcecode to get a good idea as to how everything worked 🙂 I’m going to talk to Jason Carreira to see if he’ll let me update the documentation on the xwork wiki – hopefully it’ll get more people involved in the project…

  4. Brian K. Buckley

    Thank you for the xwork example! I’d been trying to find one.

    Fix “invocation.execute()” to “proxy.execute()” above.

  5. Sam


    Thanks for that – I removed the direct use of an ActionInvocation as per recommendation from Jason, but forgot to clean the code up. I’ve fixed it now.

  6. Pat

    IoC will not work in this example; apparently there’s no component initialization using the default action proxy.. I don’t even know if this is a bug or a feature 😉 ….

  7. Sam

    Just a limitation – Jason is aware of this and is looking at improving the IoC features of XWork. In the meantime, I’ve been looking at building a similar system in Spring – see my later posts for details.

  8. Jared Odulio

    Nice way to build test cases for Action classes. Good stuff.

  9. rgml

    The example provided is very helpful. Have you tried to pass in any parameters to the action class?

    I created a HashMap and put it as a third argument to factory.createActionProxy method. However, this doesn’t work. Any idea where problem is? Perhaps I am doing it the wrong way.

    Any help from this forum would be very much appreciated. Thanks in advance.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS

%d bloggers like this: