Thursday, September 4, 2008

Action-based Web Frameworks

I'm in the process of grokking Struts 2. It's basically what I remember in Struts 1, with the addition of some nice features like annotations.

But I'm struck by the clunky action-view configuration. Yes annotations help, but why does Struts still want to map action results to views at all? Should this be a programming rather than a configuration issue? Consider the following trivial Struts example (taken from Starting Struts 2):
class MyAction {
public void String execute() throws Exception {
if( myLogicWorked() ) {
return "success";
} else {
return "error";
}
}
}

The result strings ("success" and "error") then need to be mapped to specific views. Here's how it'd be done in struts.xml:
<action name="my" class="com.fdar.infoq.MyAction" >
<result>view.jsp</result>
<result name="error">error.jsp</result>
</action>

Contrast this with the Grails/Rails "convention instead of configuration" philosophy:

def myAction = {
// do stuff
}

Grails automatically maps myAction to myAction.gsp. If the result of my action requires a different view, I simply program it that way:

def myAction = {
// do stuff
render( view:"someOtherView.gsp")
}

I guess the Struts team thinks it's a good idea to keep the action-view mappings in configuration. But I don't know why. I can't ever imagine a non-programmer diving into struts.xml to remap application flow. It seems like an unnecessary complication.