Hoople

Struts

Hoople can be used to better manage your struts-config.xml file.  Using Hoople allows you to specify the available action mappings in easy to manage files that correspond to the action mapping paths, rather than putting them all in the struts-config.xml file.

Add a “struts” tag within the URL XML file. Here is an example file that could be stored as /news/news.submit.do

<?xml version='1.0'?>

<!DOCTYPE url PUBLIC "-//SUNDOG//DTD HOOPLE//EN"
    "http://www.sundog.net/hoople/dtd/hoople_1_0.dtd">

<url>
    <struts>
        <action type="net.sundog.hoople.example.struts.action.NewsSubmitAction"
                    name="newsForm"
                    scope="request"
                    input="news.edit.html">
            <forward name="success"/>
            <forward name="failure" path="failure.jsp"/>
        </action>
    </struts>
</url>

Within “struts” tag, you specify what you normally would for that action mapping in Struts, with a few modifications.

  • You don’t need to specify the path attribute, because that can be determined by the location of the file.
  • You can use relative paths for “input” and “forward” and they will be converted to the absolute paths that struts requires when they are added to the struts-config.xml file
  • If you specify a forward without giving it a “path” attribute, the generated struts-config file will contain a path attribute that is the same as the URL, but with a JSP extension.

Create your struts-config.base.xml file. This is the same as the normal struts-config.xml but doesn’t need to include any action mappings. 

Once you have your URL XML file(s) created, add a call to hoople in your Ant build script:

<taskdef name="hoople" classname="net.sundog.hoople.ant.HoopleTask">
       <classpath refid="classpath"/>
</taskdef>

<hoople documentRoot="${document.root}">
      <include name="**/*.html"/>
      <include name="**/*.do"/>
      <struts baseFile="${document.root}/WEB-INF/struts-config.base.xml"
                        finalFile="${document.root}/WEB-INF/struts-config.xml"
                        validateByDefault="false"/>
      </hoople>

When you run your Ant task, you will get a generated standard struts-config.xml that contains everything in the struts-config.base.xml plus all your action mappings. 

It is best to not check the generated struts-config.xml into your version control system since it will be prone to conflicts as different developers generate it on a regular basis.