Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

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:

  1. 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>
    
  2. 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>
    
  3. 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 in Java at Mar 07 2007, 05:19:18 PM MST 9 Comments
Comments:

Minor improvement - you should be able to specify that 'struts.codebehind.pathPrefix' as an init-param in your web.xml as well, cutting down the total files required to just web.xml. As for the idea of merging the plugin into core, with the very positive feedback we've gotten regarding this future, I think this might be one of the rare cases where we fold a plugin into core.

Posted by Don Brown on March 08, 2007 at 01:06 AM MST #

With Seam you can use JSF without having to define your managed beans in XML. You just need to let Seam know about the component by using an @Name annotation.

Posted by 70.135.75.80 on March 08, 2007 at 02:12 AM MST #

https://spring-annotation.dev.java.net/

Posted by Tetsuo on March 08, 2007 at 03:29 AM MST #

After hearing much people talk about Stripes, I've just started evaluating it 3 days ago. And my, was I blown away by its simplicity in configuration (in fact, there's virtually nothing to configure)!

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:
  • /account/ViewAccount.jsp
  • /account/viewAccount.jsp
  • /account/view_account.jsp
Best of all, these mappings are available by default (also can be tweaked if you want to). No configuration is needed.

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 #

Right now you can't do with zero configuration all you can do with the struts.xml configuration.
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 #

[QUOTE]
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 #

In case you were wondering why your Action classes weren't being mapped properly http://arsenalist.com/2007/04/04/struts-2-and-zero-configuration-for-actions-and-results/

Posted by Sean on April 04, 2007 at 11:17 PM MDT #

Post a Comment:
  • HTML Syntax: Allowed