Suggested Improvements in the NetBeans Struts File Templates

I’ve recently created a new Java web project in NetBeans 6.5 and added the Struts framework to it. While creating lots of Struts Actions and ActionForm classes, I made a number of changes to the file templates to customize how the new Struts classes are generated. This article discusses several minor corrections I feel should be made in the out-of-the-box templates prior to the next release of NetBeans as well as some small suggested improvements.

Last summer, I was on the NetCat65 team that performed user acceptance testing for NetBeans 6.5. I got on my soapbox and submitted a Javadoc-related issue for the Struts file templates. It was corrected and added to NB6.5. You can see it here if you’re curious : http://www.netbeans.org/issues/show_bug.cgi?id=144399

I’ve since determined a number of small issues I have with the Struts file templates that would be useful to have corrected.

Suggestion #1 – Struts Action template import of org.apache.struts.action.Action. When you generate a new Struts Action class, it extends org.apache.struts.action.Action using the ${superclass} notation from the file template :

public class ${name} extends ${superclass} {

…which in my generated class looks like this:

public class NewStrutsAction extends org.apache.struts.action.Action {

This is fine, but the generated class (from the template) imports org.apache.struts.action.Action. The import is flagged by NetBeans as an unused import since the fully qualified class name is used after the extends keyword. I would argue then that perhaps the template should not import org.apache.struts.action.Action.

The consistency issue here lies in that the ActionForm template does not import org.apache.struts.action.ActionForm even though a generated ActionForm extends org.apache.struts.action.ActionForm.

Read the filed issue and vote for it here : http://www.netbeans.org/issues/show_bug.cgi?id=157954

Suggestion #2 – Correct the ordering of the Struts imports in the Action class file template. Many tools like Checkstyle and other static analysis tools report problems with classes where the import statements are not alphabetically ordered. The Struts Action template defines these imports (not included the javax.* stuff) :

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

My previous suggestion argued to remove the import of org.apache.struts.action.Action. Let’s pretend it does not exist leaving us with:

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForward;

These imports are not alphabetically ordered correctly, causing tools like Checkstyle to throw warnings. These imports should be ordered as such:

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

Read the filed issue and vote for it here : http://www.netbeans.org/issues/show_bug.cgi?id=157955

Suggestion #3 – Correct the order of modifiers for the ‘SUCCESS’ forward in the Struts Action file template:

/* forward name=”success” path=”" */
private final static String SUCCESS = “success”;

Should be:

/* forward name=”success” path=”" */
private static final String SUCCESS = “success”;

This is based on the Java Language specification in section 8.3.1 detailed here: http://java.sun.com/docs/books/jls/second_edition/html/classes.doc.html

Fixing this will result in fewer static analysis warning generated by tools like Checkstyle for people who use the file templates to generate Struts Actions.

Read the filed issue and vote for it here : http://www.netbeans.org/issues/show_bug.cgi?id=157960

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>