struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <Craig.McClana...@Eng.Sun.COM>
Subject Re: Struts--question about bean/form name.
Date Wed, 21 Jun 2000 17:03:40 GMT
David Chisholm wrote:

> Can you create a custom ActionMapping class that can store arbitrary
> information?  (This doesn't appear possible.)

You can in fact do this -- it becomes a little more work.  I followed these
steps when I added the ability to define as many <forward> elements as you want
underneath an <action>, so I will describe the steps in terms of that scenario:

* Optionally, create a new class that represents the "thing" you are going
  to be saving configuration in, if it's not just a simple string.  In my case,
  I created a new ActionForward class to represent a logical name, a
  corresponding path, and a redirect flag.

* Create an extended ActionMapping class that includes a collection (a
  Hashtable or HashMap, for example) for the new thing you want to add.
  In my case, I added a Hashtable called "forwards" to ActionMappingBase.

* In this custom ActionMapping method, create a new public method that
  adds a new entry to this collection.  In my case, I added:
        public void addForward(ActionForward forward) {
            forwards.put(forward.getName(), forward);

* Override the initMapping() method of ActionServlet with one that adds some
  additional rules to the Digester that processes the XML file.  The precise
  details here get pretty intense -- suffice it to say that I added the

            "addForward", "org.apache.struts.action.ActionForward");

  that cause the following actions whenever a <forward> element is found
  within an <action> element:

    -   Create a new object of the class specified by the "forwardClass"
        variable (normally "org.apache.struts.action.ActionForward")
        and push it on the digester's stack.
    -   On the new top stack element, map all the properties from the current
        XML element (the <forward>) into corresponding setXxxx calls.
    -   Call the addForward() method of the next-to-top element on the stack,
        passing the top element of the stack as an argument.  The method expects

        an argument of type org.apache.struts.action.ActionForward (or a
        of that class).
    -   Pop the top element (the ActionForward object) off the digester's stack.

In principle, you can do the same sort of thing for any arbitrarily nested set
of XML elements to define pretty much any configuration information that can be
statically represented as a tree of objects -- no loops or many-to-many
relationships, but pretty much anything else.  For a more complex example of
this same design approach, look at Tomcat's "conf/server.xml" file -- Tomcat
uses a very similar approach to configure itself based on the contents of a very
comprehensive configuration file.  Tomcat uses this to read the web.xml file
describing your application as well.

>  How about a new feature that
> allows you to specify the custom ActionMapping on a per action basis in the
> action.xml file.  For example:
>  <action path="/logon"
>    actionClass="org.apache.struts.example.LogonAction"
>    formAttribute="logonForm"
>    formClass="org.apache.struts.example.LogonForm"
>    inputForm="/logon.jsp"
>    actionMapping="my.custom.LogonActionMapping”
>    useDefaultHomePage=”true”>
>      <forward name="logon"      path="/logon.jsp"/>
>      <forward name="success"    path="/mainMenu.jsp"/>
>  </action>
> When parsed, the custom action mapping would be instantiated and used to
> store the custom ‘useDefaultHomePage’ attribute.

> Is this feasible?

A custom ActionMapping per action entry?  That seems like it might be reasonable
to make a standard feature of, falling back to the default one if you don't
override it.  I'll look at that scenario.

> David


View raw message