S2 Tip - Use Exception handlers

Since Action classes tend to access the same business layer, most Actions often need to catch the same set of exceptions. Rather than sprinkle Actions with nearly identical try..catch blocks, configure an Exception handler to catch whatever exceptions an Action may throw.

<struts>

<global-results>

    <result name="error">/Error.jsp</result>

  </global-results>`  <global-exception-mappings>

    <exception-mapping

      result="error"

      exception="java.lang.Throwable"/>

  </global-exception-mappings>

  <!-- ... -->

&lt;/struts&gt;</pre>
Exception handlers can be either global or local to an action mapping.
<pre>`&lt;struts&gt;

  &lt;action name="Login"  class="actions.Login"&gt;

    &lt;!-- ... --&gt;

    &lt;result name="expired" type="chain"&gt;

      ChangePassword

    &lt;/result&gt;

    &lt;exception-mapping

      exception="dao.ExpiredPasswordException"

      result="expired"/&gt;

  &lt;/action&gt;

  &lt;!-- ... --&gt;

&lt;/struts&gt;

Use of exception handlers separates concerns and reduces redundant code. Each Action has fewer lines of code to maintain, and we know that exceptions are being handled consistently.

Ready to give back? Help sponsor ApacheCon!

Newsflash:

Sponsorships for ApacheCon Europe 2007 are still available!

The conference opens May 1 in Amsterdam, The Netherlands, so time is short, but if your organization might be able to help, please contact Delia Frees at delia@apachecon.com or on +1 707 765 0823. Various sponsorship levels and other custom strategies are available. Our willingness to work with people does extend to convention sponsorship!

ApacheCon Europe 2007 is the official conference of the Apache Software Foundation (ASF). ApacheCon draws ASF Members, innovators, programmers, developers, vendors, and users to experience the future of Open Source development. Meet, mingle, and exchange ideas with like-minded participants on groundbreaking technologies and emerging industry trends, through informal networking, peer discussions, birds-of-a-feather sessions, and entertaining social events.

In related news, the US conference will be held in Atlanta GA, November 12-16, 2007. For Atlanta, I’m hoping to snag one of the whole-day training sessions or maybe an “Ajax Smackdown” presention, and then do that meet and mingle thing :)

S2 Tip - Maintain configuration files in a resource folder or Java package

The default struts.xml file is loaded from the root of the classpath. In a web application, the WEB-INF/classes folder is loaded to the root of the classpath.

To avoid maintaining files directly under WEB-INF, create a resource folder that can be copied to WEB-INF/classes when the appliication is compiled. If subfolders are also copied, then the resource folder can mirror the Java package system. The Struts Showcase application uses this common approach.

+ java

  + receivables

    - Deposit.java

    - Menu.java

  + payables

    - Menu.java

+ resources

 - payables.xml

 - receivables.xml

 - struts.xml

  + payables

    - Menu.properties

  + receivables

    - Menu.properties

+ webapp

  + payables

  + receivables

    - Deposit-error.jsp

    - Deposit-input.jsp

  + WEB-INF
Alternatively, maintain the configuration files alongside the Java packages, and have the build system copy resource files from the Java source root. The Struts Mailreader uses this alternative approach.
+ java

+ receivables

- struts.xml

- Deposit.java

- Menu.java

- Menu.properties

+ payables

- struts.xml

- Menu.java

- Menu.properties

- struts.xml

+ webapp

+ receivables

- Deposit-error.jsp

- Deposit-input.jsp

+ payables

+ WEB-INF
If FreeMarker templates are used in lieu of JSPs, then all the resources for a namespace can be kept in a single folder.
+ java

+ receivables

- struts.xml

- Deposit.java

- Deposit-input.ftl

- Deposit-error.ftl

- Menu.java

- Menu.properties

+ payables

- struts.xml

- Menu.java

- Menu.properties

- struts.xml

S2 Tips - Try to keep the Action classes for the namespace in a common Java package

_As part of the Struts 2 from Square One project, I’ve been reducing the Struts 2 tips to a manuscript. One Struts Tip a week isn’t keeping up with the manuscript, so I’ll be running two a day for the rest of the week. _

An element of Java style is to place types that are common used together into the same package. If namespaces are being used to organize an application, then the Actions within a namespace should be found in the same Java package.