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 16:22:11 GMT
David Chisholm wrote:

> On a similar note, can you associate non-standard attributes for an action
> in the action.xml, and then retrieve those values in the Action.perform()
> method?  For example, adding the useDefaultHomePage attribute shown below to
> the example logon action:
>
>   <action path="/logon"
>     actionClass="org.apache.struts.example.LogonAction"
>     formAttribute="logonForm"
>     formClass="org.apache.struts.example.LogonForm"
>     inputForm="/logon.jsp">
>     <forward name="logon"      path="/logon.jsp"/>
>     <forward name="success"    path="/mainMenu.jsp"/>
>     useDefaultHomePage="true"
>   </action>
>
> One suggestion, add a 'public void init(ActionMapping mapping)' method to
> Action that is called when the Action is instantiated to allow the Action to
> perform any static initialization that it needs, and a 'public void
> destroy()' to allow it to do any cleanup.  ActionBase would then provide
> default implementations of these methods.
>

The approach to non-standard attributes in ActionMappings is actually pretty
cool.  It works like this (documented in the User's Guide, by the way).

* Create a new class that implements ActionMapping (or extends
  ActionMappingBase, which is usually more convenient).

* In this new class, add public getter and setter methods for the
  new properties you want to define, following JavaBeans naming
  conventions.  The property type can be String, int, boolean, float,
  or double.

* In the action.xml file, add new attributes in the <action> element to
  initialize the new property values.

* In the web.xml file for your application, declare a servlet initialization
  parameter for the controller servlet that identifies the name of the
  ActionMapping class to be used (i.e. your new one):

        <init-param>
            <param-name>mapping</param-name>
            <param-value>com.mycompany.MyActionMapping</param-value>
        </init-param>

* When the controller servlet starts, it will auto-magically set all the
standard
  ActionMapping properties, plus all of your new ones.  It uses Java reflection
  to match up the property names versus the setter methods in your
  ActionMapping class.

This would seem to avoid the need to build your own init() method in an
ActionMapping simply to configure non-standard property values.

>
> David
>

Craig McClanahan



Mime
View raw message