You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Didier Carlier <Di...@sema.be> on 2000/09/26 18:48:49 UTC

Variable number of fields in a form

Is it possible to set-up a form with a variable number of fields ?

An example of what I would like to do is the "Current Subscriptions"
table of the example application, except that editing the various fields
would be done "in place".
Each time a new subscription is added, a new line is added to the table,
and hence 3 new input fields for the host name, user name and server
type.

I can possibly generate unique names for the fields within an iterate or
enumerate tag, but I don't see how to populate the form bean.

Thanks for any hint/suggestion.
Didier

Re: Variable number of fields in a form

Posted by Didier Carlier <Di...@sema.be>.
 
> There are two types of indexed proeprties with different signatures:
> 
> bean.getProperty1( Value[] ) - which used as setter and as getter
> 
> and
> 
> bean.setProperty( int index, Value value )
> bean.getProperty( int index )
> 
> And in main idea you can use both methods with
> <input name="property[1]"...>
> But now struts have not support for processing indexed properties in
> ActionServlet and you can only view, not pass to process this
> properties.

Looking again at the code, and after testing it, it works just fine 
as it is now. In my jsp page I have:

 <logic:iterate id="item" name="nodedata" property="lines" >
  ...
    <input type="text" name="lines" value="<%= item.toString() %>">
  ...
 </logic:iterate>

and my bean has the method
 
public void setLines(String[] lines) {
   	this.lines=lines;
 }

The BeanUtils code is smart enough to properly handle the case when
multiple
fields have the same name.

*Very* nice.
Best regards,
Didier 

Re[2]: Variable number of fields in a form

Posted by Oleg V Alexeev <go...@penza.net>.
Hello Didier,

Tuesday, September 26, 2000, 2:46:10 PM, you wrote:

 
>> Indexed properties is a best way to solve this problem. Now struts
>> supports indexed properties in <logic:* and in <bean:* tags only. No
>> ActionServlet, no old <struts:* library have not support of this
>> feature. I extend ActionServlets and make some work to support indexed
>> properties in old library. After that I can use forms with variable
>> length - easy and fun.

DC> Browsing the Beanutils code, I have the impression that the idea was
DC> to have the populate method handling input fields of the form
DC> <input name="property[0]" ..>
DC> <input name="property[1]"...>

DC> and then call a bean.setProperty(Value[]) method.
DC> That would nicely solve my problem. Is it worth trying to go in that 
DC> direction ?

There are two types of indexed proeprties with different signatures:

bean.getProperty1( Value[] ) - which used as setter and as getter

and

bean.setProperty( int index, Value value )
bean.getProperty( int index )

And in main idea you can use both methods with
<input name="property[1]"...>
But now struts have not support for processing indexed properties in
ActionServlet and you can only view, not pass to process this
properties.

-- 
Best regards,
 Oleg                            mailto:gonza@penza.net



Re: Variable number of fields in a form

Posted by Didier Carlier <di...@sema.be>.
 
> Indexed properties is a best way to solve this problem. Now struts
> supports indexed properties in <logic:* and in <bean:* tags only. No
> ActionServlet, no old <struts:* library have not support of this
> feature. I extend ActionServlets and make some work to support indexed
> properties in old library. After that I can use forms with variable
> length - easy and fun.

Browsing the Beanutils code, I have the impression that the idea was
to have the populate method handling input fields of the form
<input name="property[0]" ..>
<input name="property[1]"...>

and then call a bean.setProperty(Value[]) method.
That would nicely solve my problem. Is it worth trying to go in that 
direction ?

Regards,
Didier 
 


Re: Variable number of fields in a form

Posted by Oleg V Alexeev <go...@penza.net>.
Hello Didier,

Indexed properties is a best way to solve this problem. Now struts
supports indexed properties in <logic:* and in <bean:* tags only. No
ActionServlet, no old <struts:* library have not support of this
feature. I extend ActionServlets and make some work to support indexed
properties in old library. After that I can use forms with variable
length - easy and fun.
Now I use standart struts construction -

<struts:text
 name="actionForm"
 property='<%= "indexedValue[" +
                Integer.toString( index ) +
                "]" %>'/>
                
  and indexed property indexedValue processed in standart struts way.
                
I don't like it and now I want to add additional parameter to this tag
- index. With it above tag can be written as -

<struts:text
 name="actionForm"
 property="indexedValue"
 indexName="index"          -- name of bean with index
 indexProperty="value" />   -- property, which can be casted to
                               numeric value


-- 
Best regards,
 Oleg                            mailto:gonza@penza.net

DC> Is it possible to set-up a form with a variable number of fields ?

DC> An example of what I would like to do is the "Current Subscriptions"
DC> table of the example application, except that editing the various fields
DC> would be done "in place".
DC> Each time a new subscription is added, a new line is added to the table,
DC> and hence 3 new input fields for the host name, user name and server
DC> type.

DC> I can possibly generate unique names for the fields within an iterate or
DC> enumerate tag, but I don't see how to populate the form bean.

DC> Thanks for any hint/suggestion.
DC> Didier



Re: Variable number of fields in a form

Posted by Tom Miller <tm...@kdsi.net>.
I've got the same problem staring me in the face. I'm thinking at this point
to have a collection defined in the form bean, and do the multiple fields in
Javascript arrays. Then I would have a Javascript button named "Submit" call
a Javascript function just before doing the actual submit, and package the
fields into a collection. It may wind up taking some actual Javascript on
the page (which could become a custom tag I suppose) and/or even a
scriptlet. This is theoretical at the moment. I would also appreciate any
further design thoughts before lunging into the fray.

Tom

Didier Carlier wrote:

> Is it possible to set-up a form with a variable number of fields ?
>
> An example of what I would like to do is the "Current Subscriptions"
> table of the example application, except that editing the various fields
> would be done "in place".
> Each time a new subscription is added, a new line is added to the table,
> and hence 3 new input fields for the host name, user name and server
> type.
>
> I can possibly generate unique names for the fields within an iterate or
> enumerate tag, but I don't see how to populate the form bean.
>
> Thanks for any hint/suggestion.
> Didier