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.