You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Craig R. McClanahan" <cr...@apache.org> on 2001/06/02 04:28:16 UTC

Re: How can I use in stru ts-config.xml


On Fri, 11 May 2001, David Holland wrote:

> How can I use <set-property property="foo" value="123" /> in
> struts-config.xml ?
> 
> I want to use it in place of forward to store Action configuration info
> (set-property is
> specified in the dtd).
> 
> Tried to add getter & setter to Action descendant - but assignment by
> introspection 
> didn't seem to work. 
> 
> Looked at the source for ActionServlet, and the digester class does appear
> to be doing something
> with this property, although it looks like it is only looking for a single
> instance in an action.
> 
> As far as I can tell, either it is not intended for general use or it is
> broken.
> 
> Any thoughts?
> 
> David Holland
> davidh@supplywave.com
> 
> 
The basic idea is that you might want to subclass ActionMapping (the
Struts class that holds the information related to an <action> element) to
add some additional properties.  We still want to take advantage of the
automatic property setting that Digester does for you, but it is not
convenient to change the DTD for the configuration file for each
individual application requirement.

So, to support customizable property setting, the following steps are
required:

(1) You must create a subclass of ActionMapping with extra property
    getter and setter functions for your custom properties:

      public MyActionMapping extends ActionMapping {
          public String getFoo() { ... }
          public void setFoo(String foo) { ... }
      }

(2) You must tell Struts to use your MyActionMapping class (instead of
    the standard one when it processes the struts-config.xml file.  You
    can do this globally for all <action> elements by setting up the
    "mapping" servlet initialization parameter for the controller servlet:

      <servlet>
        <servlet-name>Controller</servlet-name>
        <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
        ... other init parameters ...
        <init-param>
          <param-name>mapping</param-name>
          <param-value>com.mycompany.mypackage.MyActionMapping</param-value>
        </init-param>
      </servlet>

    or you can override the class to be used on each individual action:

      <action ...
       className="com.mycompany.mypackage.MyActionMapping" ... />

(3) Now, in your struts-config.xml file, you can customize the value of
    "foo" for each individual <action> element.  Assuming you did the
    global naming alternative, you might have something like this:

      <action path="/bar"
              type="com.mycompany.mypackage.BarAction">
        <set-property property="foo" value="Custom Foo Value"/>
      </action>

(4) To retrieve the custom configuration values inside your action,
    you will need to cast the ActionMapping you receive to your own
    value:

      public ActionForward perform(ActionMapping mapping, ...) ... {
        MyActionMapping myMapping = (MyActionMapping) mapping;
        String foo = myMapping.getFoo();
        ...
      }

The same basic design pattern lets you customize the implementation
objects for <forward> and <form-bean> declarations as well, by extending
ActionForward and ActionFormBean respectively.

Craig McClanahan