Populating a List in a Jakarta Struts DynaActionForm

T

Thomas

Hi,

I have a question concerning DynaActionForms in Jakarta Struts 1.1

In my struts-config.xml I declare the following DynaActionForm and I
would like to know if it is possible to prepopulate the ArrayList
"cities" in struts-config.xml with some String objects containing city
names, is that possible at all and if yes, can someone send me the xml
code that does it?

I am not looking for an alternative solution with a custom ActionForm
or something like this, it's just an example and I just want to know
if this is possible to do with DynaActionForms, so if the answer is
"no" that's I am satisfied too.

Thanks in advance

Thomas


excerpt from my struts-config.xml
=================================

<form-bean name="personaldataForm"
type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="firstname" type="java.lang.String"
initial="someName" />
<form-property name="lastname" type="java.lang.String"
initial="someothername" />
<form-property name="street" type="java.lang.String"
initial="somestreet" />
<form-property name="zip" type="java.lang.String" initial="12345"
size="5" />
<form-property name="city" type="java.lang.String"
initial="somecity" />
<form-property name="page" type="java.lang.Integer" />
<form-property name="cities" type="java.util.ArrayList">
</form-property>
</form-bean>
 
T

Thomas

Probably. Look at the "initial" attribute of the said:
using it to set initial values for single valued fields. I imagine you
could do it for a select list that allows multiple selections, so it should
also work for a String[] you intend to use as the options rather than the
selected values. At the point of constructing the Form bean, Struts has no
idea what you're going to do with it.

Well, I also think that I have to do it with the initial attribute,
but I just don't know the syntax. I couldn't find any example for this
case.
The answer isn't so much "no" as "why would you want to"? The Form bean is
meant to carry the user's input back and forth from the browser to the
framework. The particular city the user has requested belongs in the Form
bean. The list of cities he can choose from does not. Especially since you
may have "city" on more than one form.

Well the case, that I thought of is the initialization of comboboxes
with a collection that is a property of the ActionForm using the
"property" attribute of the options Tag in the Struts HTML Taglib.
It's possible with custom ActionForms and I just wanted to know if I
could do it with DynaActionForms too. If that makes sense or is good
design is another question. I wouldn't do it like that in a real live
application.

Cheers,

Thomas
 
J

Joe Bloggs

I have the exact same issue as you do. My experience is that you cannot do
that. Your value object can contain a List but you cannot define your List
in your form bean inside struts-config.xml.

Problem is then, how on earth do we create dynamic number of checkboxes
(multiboxes) - so far I haven't found "recommended" answers. Perhaps you
might want to send Ted Husted an email :)

Anyway, my solution, albeit ugly, is to have a hidden field which register
the checkboxes' values separated by commas. You would also need to add your
custom javascript validations and the backend (Java) validation somewhere in
the controller or inside some java classes.

yaks!, some of you might think but unless someone can suggest a better
solution, my workaround will have to stick for a while.

cheers,

Thomas said:
Probably. Look at the "initial" attribute of the <form-property> tag. I'm
using it to set initial values for single valued fields. I imagine you
could do it for a select list that allows multiple selections, so it should
also work for a String[] you intend to use as the options rather than the
selected values. At the point of constructing the Form bean, Struts has no
idea what you're going to do with it.

Well, I also think that I have to do it with the initial attribute,
but I just don't know the syntax. I couldn't find any example for this
case.
The answer isn't so much "no" as "why would you want to"? The Form bean is
meant to carry the user's input back and forth from the browser to the
framework. The particular city the user has requested belongs in the Form
bean. The list of cities he can choose from does not. Especially since you
may have "city" on more than one form.

Well the case, that I thought of is the initialization of comboboxes
with a collection that is a property of the ActionForm using the
"property" attribute of the options Tag in the Struts HTML Taglib.
It's possible with custom ActionForms and I just wanted to know if I
could do it with DynaActionForms too. If that makes sense or is good
design is another question. I wouldn't do it like that in a real live
application.

Cheers,

Thomas
 
J

Joe Bloggs

Hi,
This solution is only applicable for forms that uses Form beans but not
DynaActionForm or DynaValidateForm.

Secondly, it also works because there is only one property (the Select
field) to pass back to the server upon a submit.

In the case of multiple checkboxes where the number of checkboxes that will
appear on a form is onlyn known during runtime, the solution wouldn't cut
it.

I suppose one can predefined an abitrary number of boolean/string properties
(option1...optionN) in the value object to hold the checkboxes value, and
check retrieves these properties in java classes.

cheers,
 
S

Sudsy

Joe said:
Hi,
This solution is only applicable for forms that uses Form beans but not
DynaActionForm or DynaValidateForm.

Secondly, it also works because there is only one property (the Select
field) to pass back to the server upon a submit.

In the case of multiple checkboxes where the number of checkboxes that will
appear on a form is onlyn known during runtime, the solution wouldn't cut
it.

I suppose one can predefined an abitrary number of boolean/string properties
(option1...optionN) in the value object to hold the checkboxes value, and
check retrieves these properties in java classes.

You're half right: it doesn't work with DynaActionForm and classes
which extend it. It works just peachy with checkboxes, radio boxes,
etc. I'm working on adding some additional examples to the documentation
but I merely extended the mechanism to include checkboxes. It took me
all of a half-hour to code and test.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top