Zero Configuration in Struts 2
Struts 2 has a nifty zero configuration feature. However, it's only useful for registering actions, not for automatically registering results. In other words, you still have to use an @Result annotation to tell your action what page to dispatch to. To use default view names instead of requiring @Result, you can use the Codebehind Plugin. Also, did you know Struts 2 will autowire your Spring dependencies? It's pretty slick.
What does this all mean? It means you can write your Struts 2 application without writing any XML. Of course, you can still use XML to tweak behavior, but with these plugins enabled, you won't have to.
IMO, these plugins should be combined into a single zero configuration feature.
Here's how you can enable Struts 2's Zero Configuration feature in AppFuse 2.0:
- Add a packageNames parameter to the "struts" filter in your web.xml:
<filter> <filter-name>struts</filter-name> <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> <init-param> <param-name>actionPackages</param-name> <param-value>com.company.newapp.webapp.action</param-value> </init-param> </filter>
- Add the Codebehind Plugin as a dependency in your pom.xml:
<dependency> <groupId>org.apache.struts</groupId> <artifactId>struts2-codebehind-plugin</artifactId> <version>2.0.6</version> </dependency>
- Add a struts.codebehind.pathPrefix constant in struts.xml for your default pages directory:
<constant name="struts.codebehind.pathPrefix" value="/WEB-INF/pages/"/>
That's it - now you can code away without configuring anything!
How does this compare to other web frameworks in AppFuse? Tapestry has a similar feature, but Spring MVC and JSF don't. Spring MVC still requires you create a bean definition for Controllers and JSF requires you write a chunk of XML for each managed bean. Of course, if you know how to do something similar with Spring MVC or JSF, please let me know.
Posted by Don Brown on March 08, 2007 at 01:06 AM MST #
Posted by 70.135.75.80 on March 08, 2007 at 02:12 AM MST #
Posted by Tetsuo on March 08, 2007 at 03:29 AM MST #
One of Stripes' key features is: Zero external configuration per page/action (ActionBeans are auto-discovered, and configured using annotations)
An example:
Suppose you have an action class com.myapp.action.account.ViewAccountAction, you can already access it via URL http://<hostname and context>/account/ViewAccount.action.
No time to code the action class yet? No worries - if the action class is missing, Stripes will then check for JSPs in the following order and serve it out if present:
Posted by Sing Chyun on March 08, 2007 at 04:52 AM MST #
It looks like the people responsible for the latest crop of webapp frameworks have decided that ease-of-use really does matter, be it via annotations, convention, or both.
I've been playing with Tapestry 5 a lot lately, and this seems to be the main focus of the release. Howard's calling it "adaptive API": component event handlers and lifecycle methods can either be annotated as such, or just conform to some naming scheme (i.e. "on[event-name]From[component-id]"); component properties can be "coerced" into other types automatically as needed (i.e. you can use either a SelectModel, a Map, or a List as the model for a Select component); etc.
It'll be interesting to see how far all these frameworks can go in this direction without annoying the user with incorrect assumptions as to their intentions.
Posted by Daniel Gredler on March 08, 2007 at 05:21 AM MST #
I want to have several methods in my Action class, an to be able to call each method with a different URL:
<action name="action1" class="com.carre.action.TestAction" method="action1">...
<action name="action2" class="com.carre.action.TestAction" method="action2">...
<action name="action3" class="com.carre.action.TestAction" method="action3">...
I don't think there is a way to do that with zero conf as of Struts 2.0.6.
And yes, I also think they should merge zero conf with the codebehind plugin.
Posted by Guillaume Carre on March 08, 2007 at 10:18 AM MST #
Of course, if you know how to do something similar with Spring MVC [...], please let me know
[/QUOTE]
Matt, you can open a Jira ticket on Spring Modules and ask for such a feature to be implemented into the SpringMVC-extra module ;)
Cheers!
Sergio B.
Spring Modules Team
Posted by Sergio Bossa on March 10, 2007 at 02:43 PM MST #
You actually could do the same thing in Struts 1. At one point in time, I had a custom module that would use the Apache Commons Discovery library to look for classes that implemented an interface. You could then get a handle on the Struts action mappings configuration and add your own mappings programatically, if I remember correctly.
You couldn't do complex action mappings, but it was possible to wire it up, if you used a custom module (you needed a handle on the internal struts config from the module initialization).
I point this out just to say that this isn't as revolutionary as it sounds, but it's more a question of having a known configuration or convention. Once you have sensible defaults that can be configured automatically, this becomes much easier. Annotations actually make this quite simple.
Posted by Marcus Breese on March 14, 2007 at 05:52 AM MDT #
Posted by Sean on April 04, 2007 at 11:17 PM MDT #