You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by Christian Gleissner <od...@odn.de> on 2001/08/12 14:04:16 UTC

Random access to indexed properties

Hello,

a while back, someone asked why the struts ActionForm class lacks 
support for dynamic parameters. 

I don't believe it is necessary to extend Struts so that it maps
any kind of dynamic parameter to a certain property. This should
be best done in an Action. 

However, this posting made me think
about a deficiency that I noticed: Struts currently lacks support
for random access to the indexed properties of an ActionForm.

In this email, I will first present the issue and
then suggest a possible solution that could be 
incorporated into the next release of Struts.


Issue
#######

Short description 
-----------------

ActionForm doesn't support random access to indexed properties. Random
access is however important for dynamically generated forms since:
 - a certain property order is not guaranteed
 - properties may contain gaps


Long description
----------------

In our web application we need to set an indexed bean property. However,
the problem is that the indices of the property are not continually
set. Instead, some may be set and others may not be set. On top, 
the order in which these parameters are packed in the http request 
is not the same order in which they need to be set on the bean.

Let's suppose we have the parameter string ?state=foo&state=bar

JSP's setProperty tag (and I suppose also Struts' usage of 
ActionForm classes)  transforms this into one call of the indexed 
property "state":
public setState(Object[] state);

Therefore, the following mapping applies:
   0 -> foo
   1 -> bar

However, I have no means to inform Struts to set indices 2 and 0 instead
so that the bean property would look like this:
   0 -> bar
   1 -> <not changed by request>
   2 -> foo

Such gaps are possible since the a dynamically generated
form might prevent access to certain elements. Moreover, the order 
in which the elements are shown may not coincide with the order 
in which the indexed property expects them.


Possible solution
#################

I suggest extensions to both struts-config.xml and the class that
introspects on ActionForm.

Introspection
-------------

An example:
We have a dynamically generated form. If the form is submitted, 
each value is transmitted in the following form:
<property><index>=<value>

Let us assume our property is "state". Then a possible request
string might be

state2=4&state0=7

These values should now be passed to the accessor for the indexed
property bean:
public void setState(int index, int value)
(This interface conforms to the JavaBean convention.)

This means, the class that is responsible for setting ActionForm
classes (whichever that is) needs to know:

 - that state0 and state1 are not properties themselves but a mixture
   of property name and index

 - that it needs to convert "4" and "7" to int

In our example, the generated calls would be
setState(2, 4);
setState(0, 7);


Configuration
-------------

In struts-config.xml, the <form-bean> tag is extended to specify
indexed properties.

<form-bean name="barForm" type="foo.BarForm" indexed-property="state"/>

In order to deal with multiple indexed properties for a form, 
the following extension is possible.

<form-bean name="barForm" type="foo.BarForm">
  <indexed-properties>
    <indexed-property>property1</indexed-property>
    <indexed-property>property2</indexed-property>
  <indexed-properties>
</form-bean>


What do you think about this?

Best,
Christian



Re: Random access to indexed properties

Posted by Erik Hatcher <er...@earthlink.net>.
Christian,

Seems like you're re-inventing the wheel..... Struts 1.1 includes "indexed"
capability, but even with 1.0 you could just name bracket the indexes so
they come out in HTML like this:

    <input  type="text" name="option[1]">

which will cause the indexed setter to be called when the form is submitted.

Or am I missing something from what you are getting at?

    Erik



----- Original Message -----
From: "Christian Gleissner" <od...@odn.de>
To: <st...@jakarta.apache.org>
Sent: Sunday, August 12, 2001 5:04 AM
Subject: Random access to indexed properties


> Hello,
>
> a while back, someone asked why the struts ActionForm class lacks
> support for dynamic parameters.
>
> I don't believe it is necessary to extend Struts so that it maps
> any kind of dynamic parameter to a certain property. This should
> be best done in an Action.
>
> However, this posting made me think
> about a deficiency that I noticed: Struts currently lacks support
> for random access to the indexed properties of an ActionForm.
>
> In this email, I will first present the issue and
> then suggest a possible solution that could be
> incorporated into the next release of Struts.
>
>
> Issue
> #######
>
> Short description
> -----------------
>
> ActionForm doesn't support random access to indexed properties. Random
> access is however important for dynamically generated forms since:
>  - a certain property order is not guaranteed
>  - properties may contain gaps
>
>
> Long description
> ----------------
>
> In our web application we need to set an indexed bean property. However,
> the problem is that the indices of the property are not continually
> set. Instead, some may be set and others may not be set. On top,
> the order in which these parameters are packed in the http request
> is not the same order in which they need to be set on the bean.
>
> Let's suppose we have the parameter string ?state=foo&state=bar
>
> JSP's setProperty tag (and I suppose also Struts' usage of
> ActionForm classes)  transforms this into one call of the indexed
> property "state":
> public setState(Object[] state);
>
> Therefore, the following mapping applies:
>    0 -> foo
>    1 -> bar
>
> However, I have no means to inform Struts to set indices 2 and 0 instead
> so that the bean property would look like this:
>    0 -> bar
>    1 -> <not changed by request>
>    2 -> foo
>
> Such gaps are possible since the a dynamically generated
> form might prevent access to certain elements. Moreover, the order
> in which the elements are shown may not coincide with the order
> in which the indexed property expects them.
>
>
> Possible solution
> #################
>
> I suggest extensions to both struts-config.xml and the class that
> introspects on ActionForm.
>
> Introspection
> -------------
>
> An example:
> We have a dynamically generated form. If the form is submitted,
> each value is transmitted in the following form:
> <property><index>=<value>
>
> Let us assume our property is "state". Then a possible request
> string might be
>
> state2=4&state0=7
>
> These values should now be passed to the accessor for the indexed
> property bean:
> public void setState(int index, int value)
> (This interface conforms to the JavaBean convention.)
>
> This means, the class that is responsible for setting ActionForm
> classes (whichever that is) needs to know:
>
>  - that state0 and state1 are not properties themselves but a mixture
>    of property name and index
>
>  - that it needs to convert "4" and "7" to int
>
> In our example, the generated calls would be
> setState(2, 4);
> setState(0, 7);
>
>
> Configuration
> -------------
>
> In struts-config.xml, the <form-bean> tag is extended to specify
> indexed properties.
>
> <form-bean name="barForm" type="foo.BarForm" indexed-property="state"/>
>
> In order to deal with multiple indexed properties for a form,
> the following extension is possible.
>
> <form-bean name="barForm" type="foo.BarForm">
>   <indexed-properties>
>     <indexed-property>property1</indexed-property>
>     <indexed-property>property2</indexed-property>
>   <indexed-properties>
> </form-bean>
>
>
> What do you think about this?
>
> Best,
> Christian
>
>