I’ve been playing around with “XWork”:http://wiki.opensymphony.com/space/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.
Scenario
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”:http://wiki.opensymphony.com/space/Xwork+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) {
//handle
}
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”
Very nice example! Nice to see an XWork example outside of the WebWork wrapper! Great work!
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.
Juan,
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…
Thank you for the xwork example! I’d been trying to find one.
Fix “invocation.execute()” to “proxy.execute()” above.
Brian,
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.
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 😉 ….
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.
Nice way to build test cases for Action classes. Good stuff.
Glad to be of service 🙂
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.