You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Kevin Wang <KW...@Partnerware.com> on 2000/11/27 21:52:54 UTC

How to make ActionForm handle extended properties

Currently, every form field has to have equivalent field in ActionForm bean,
with both getter and setter method. This requires that a new ActionForm be
generated for each new Struts form (with additional fields).

Our applicaiton requires 'extensibility', ie. HTML/JSP designer should be
able to add new fields to the form without breaking serverside Java code,
including Struts. 

I've made some minor changes to Struts 0.5 to achieve such extensibility.
Basically, it use a Hashtable to save and retrieve extra fields that do not
appear in the ActionForm.

1. Add (Hashtable) properties to BaseActionForm which implements
ValidatingActionForm, with getter and setter methods like,

    public String get(String name);

    public void set(String name, String value);

  All my ActionForm classes will extend this BaseActionForm.

2. Added a new util class that has two methods that will call the above
methods with the following interface:

    public static String getPropertyValue(Object bean, String name)
        throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException;

    public static void setPropertyValue(Object bean, String name, Object
value)
        throws IllegalAccessException, InvocationTargetException,
NoSuchMethodException;

3. Modify org.apache.struts.util.BeanUtils' get/setPropertyValue() to call
my custom get/setPropertyValue() above whenever NoSuchMethodException was
thrown.


This approch worked so far but not very elegant. The need to modify
BeanUtils.java is, of cause, troublesome. Any suggestions?

Thanks.

Kevin

Re: How to make ActionForm handle extended properties

Posted by Jim Richards <gr...@cyber4.org>.

> Currently, every form field has to have equivalent field in ActionForm bean,
> with both getter and setter method. This requires that a new ActionForm be
> generated for each new Struts form (with additional fields).
> 
> Our applicaiton requires 'extensibility', ie. HTML/JSP designer should be
> able to add new fields to the form without breaking serverside Java code,
> including Struts.
> 
> I've made some minor changes to Struts 0.5 to achieve such extensibility.
> Basically, it use a Hashtable to save and retrieve extra fields that do not
> appear in the ActionForm.

This is something I really need. I have my user profile fields generated
from a database template table, so that it might contain data for
a "First Name", "Last Name", "Address 1", "Address 2". TO make it reusable
the next application might use "Full Name" and the application knows
how to read the template table and display the correct type of 
text field and JavaScript validation function.

I thought using String[] getXXX and setXXX(String[]) methods
might have worked, but didn't.