You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matthew Hughes <ma...@gmail.com> on 2005/02/24 17:03:44 UTC

ActionForms and Interfaces

While reading through the DynaActionForm debate, I wondered if using
Interfaces as well as inheritance might be a good alternative for
those who want type enforcement but don't want to duplicate common
form elements.  I inherited a fairly large struts application months
ago with 30+ forms.  However, I have recently realized that none of
the forms use inheritance at all to share common attributes even
though there are glaring examples of forms just waiting to be
combined: ie:

PrintDocumentForm
PrintMultiPageDocumentForm
PrintWorkflowDocumentForm

The three forms share 90% of the same attributes, but no one took the
time to create a superclass.


In addition to the benefit of checking data typing, I like having
superclasses for common forms because it ensures that I am going to
call the same property the same name on multiple forms.  One of the
major headaches of this project is that there are many forms that
refer to the same bean property but are named slightly differently
(i.e., container_id, _container_id, containerId).

While inheritance will take you most of the way in solving naming
conflicts and type conflicts, I am trying to use interfaces to take me
the rest of the way.  In the form example above, I have created three
interfaces:

Document
MultiPageDocument
WorkflowDocument

and four forms

PrintForm
  PrintDocument extends PrintForm implements Document
  PrintMultiPageDocument extends PrintForm implements MultiPageDocument
  PrintWorkflowDocument extends PrintForm implements WorkflowDocument

Every bean property on the three subclass forms are either inherited
or implemented from an interface.  This way I can avoid naming
conflicts by only naming a property that is used across multiple forms
only once.

Has anyone else done something similar?  Or is there a better solution
to this problem that would use less code?

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Re: ActionForms and Interfaces

Posted by Craig McClanahan <cr...@gmail.com>.
IIRC, there's some ongoing work to provide an "extends" capability in
DynaActionForm definitions.  In the mean time, though, you can
leverage the fact that struts-config.xml is an XML document, and use
XML entities to encapsulate the common chunks.  Consider:

    <?xml version="1.0" ?>
    <DOCTYPE struts-config ... [
        <!ENTITY shared-props-1  "shared-properties-1.xml">
        <!ENTITY shared-props-2  "shared-properties-2.xml">
    ]>
    <struts-config>
        ...
        <form-bean name="myBean" type="...">
            <form-property>...unique property ...</form-property>
            &shared-props-1;
            &shared-props-2;
        </form-bean>
        <form-bean name="anotherBean" type="...">
            <form-property>...unique property ...</form-property>
            &shared-props-1;
        </form-bean>
        ...
    </struts-config>

Craig

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org