Webwork is an MVC framework made by those nice people over at Open Symphony. It's Open Source and a doddle to learn. It's so easy, I'm going to introduce it to you over a series of blog entries. So, here goes nothing.
Before we begin, perhaps it'll be useful to know how all the bits and pieces within Webwork 2 fit together. Essentially, Webwork builds in web support for XWork, a generic command framework. XWork provides support for such pleasentries as declarative validation of content and the use of these things called interceptors (which I'll come to in another blog entry: for now, think "XWork Action is to Servlet" as "XWork Interceptor is to Filter" and you've got the idea)
So, XWork is basically an implementation of the Gang of Four's command pattern. The implementation rests on an interface called "Action" (or "com.opensymphony.xwork.Action" if we're being formal) which has a single method with the signature:
public String execute() throws Exception
When XWork calls this method, it uses the returned String to map to a view. How does that work? Simple. XWork uses a file found on the classpath called "xwork.xml" to configure itself (which means that in a web app, it'll often be found in WEB-INF/classes) This file, at it's most basic, looks like:
<!DOCTYPE xwork PUBLIC "-//OpenSymphony Group//XWork 1.0//EN" "http://www.opensymphony.com/xwork/xwork-1.0.dtd">
<xwork>
<include file="webwork-default.xml"/>
<package name="some-name" namespace="" extends="webwork-default">
<action name="woot" class="fully.qualified.class.name">
<result name="success">joy.jsp</result>
</action>
</package>
</xwork>
What does all this mean? Basically, it says that when a request for "woot.action" (because ".action" is often bound to execute XWork actions, as ".do" is often used in Struts applications) XWork should instantiate a new instance of the class "fully.qualified.class.name" and call the "execute" method. Should this method return "success", then XWork will forward (not redirect) the request to "joy.jsp".
For now, let's not worry ourselves about the "include" (hint: it includes another configuration file, in this case from the "webwork2.jar") or the "package" declarations (hint: it's a mechanism to logically group actions together) Instead, marvel at how the action tag contains one (or more) nested "result" tag(s). The "name" attribute of the result tag matches the return value of the "execute" method, and the contents of the tag point to the view that should be rendered. How easy is that? That's a rhetorical question, but the answer would be "pretty damn easy" if it wasn't.
One interesting thing to note is that each request creates a new action object. This has the handy side-effect of meaning that your action's do not need to be thread-safe, and because object creation is relatively cheap the effect on the system's efficiency isn't shattering. Which is nice.
Right, I better get back to my day job. You've got an idea of what XWork is and how it relates to Webwork, and how it routes to a different view depending on what the return value of the execute method. Next time, I'll go over actually packaging up a Webwork application so that it can be deployed with minimal grief. I might even tell you more about interceptors....