After spending quite sometime on the collection-backed
form properties solution, I figured its worth
mentioning the solution I dug out. I have to say that
the documentation on this one is pretty sparse. Ok
here we go...
I have a jsp that needs to accept an order for items.
This jsp has single-occurence fields like customer
name and address and multi-occurence fields like item
id, item name and item price. The later is the focus
of this message. I have created a plain java bean for
the multi-occurence fileds called ItemBean.
public class ItemBean {
public ItemBean() {
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return this.itemId;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return this.name;
}
public void setPrice(double price) {
this.price = price;
}
public double getPrice() {
return this.price;
}
}
I have created a DynaActionForm bean as follows:
<form-bean name="orderForm" dynamic="true"
type="org.apache.struts.action.DynaActionForm">
<form-property name="name" type="java.lang.String"/>
<form-property name="address"
type="java.lang.String"/>
<form-property name="itemBeans"
type="java.util.ArrayList"/>
<form-property name="event"
type="java.lang.String"/>
</form-bean>
And I have a jsp like shown below:
<html:form action="view2Submit.do">
Name: <html:text property="name"/>
Address: <html:text property="address"/>
<table>
<tr>
<td>Id:</td>
<td>Name:</td>
<td>Price:</td>
</tr>
<logic:iterate id="itemBeans" indexId="idx"
name="orderForm" property="itemBeans"
type="com.xyz.ItemBean">
<tr>
<td><html:text name="itemBeans" property="id"
indexed="true"/></td>
<td><html:text name="itemBeans" property="name"
size="50" indexed="true"/></td>
<td><html:text name="itemBeans" property="price"
indexed="true"/></td>
</tr>
</logic:iterate>
<tr>
<td><html:submit value="Save"
onclick="setSubmitEvent('saveActivity');"/>
</td>
<td><html:submit value="Add"
onclick="setSubmitEvent('addActivityProperty');"/>
</td>
</tr>
</table>
<html:hidden property="event"/>
</html:form>
Notice that the values of "id" and "property"
attributes of the <logic:iterate> tag are the same.
This is what makes the name of the html text fields to
evaluate to the right format, which is:
itemBeans[n].id for the id field. This "itemBeans" is
the name of the indexed-property in the form bean. The
"event" field is used by DispatchAction.
Hope this was helpful to atleast someone.
-Harish
Heligon Sandra <[EMAIL PROTECTED]> wrote:
I have an other question if I use a sub-class of DynaValidatorForm
and define the form-bean in the struts-config file with:
dynamic="true" type="foo.bar.YourDyanForm">
How do I write my JSP page ?
type="foo.bar.YourDyanForm">
Is this the good manners to make?
----------------------------------------------------------------------------
As of February 12th, 2003 Thomson unifies its email addresses on a worldwide
basis.
Please note my new email address: [EMAIL PROTECTED]
http://www.thomson.net/
----Original Message-----
From: Heligon Sandra
Sent: 18 March 2003 13:43
To: 'Struts Users Mailing List'
Subject: RE: How define a DynaActionForm to display a collection ?
Importance: High
If I define a sub-class of DynaValidatorForm I just have to add
two methods to access to the list parameter, isn't it ?
public final class MyDynaForm extends DynaValidatorForm {
private ArrayList list;
public MyDynaForm ()
{
list = new ArrayList();
ConfProp props = new ConfProp();
Vector v = props.getMeterHosts();
for (int i=0; i list.add(v.elementAt(i));
setList(list);
}
public ArrayList getList() {
return list;
}
public void setList(ArrayList list) {
this.list = list;
}
}
I will wish to be certain that it is the best way to display a
collection in DynaValidatorForm
because I have a doubt in a message of the archives I read:
The only reason you would ever need to create a real class that extends
DynaActionForm or DynaValidatorForm would be if you need a custom
reset()
or validate() method.
What do you think about it?
-----Original Message-----
From: Rick Reumann [mailto:[EMAIL PROTECTED]
Sent: 11 March 2003 18:18
To: Struts Users Mailing List
Subject: Re: How define a DynaActionForm to display a collection ?
On Tue, 11 Mar 2003 17:36:09 +0100
Heligon Sandra wrote:
> Has someone an example of struts-config.xml file and
> a such SetUpAction with DynaActionForm and Collection ?
In a form-bean declarations, where YourDyanForm is a subclass of a
DynaActionForm(DynaValidatorForm):
dynamic="true" type="foo.bar.YourDyanForm">
{ snip }
{ snip }
Then in you Action class...
execute(..) {
ArrayList someList = someBusinessClass.getMeListOfObjectsIneed();
formBean.set("myCollection" someList );
//forward to jsp page
}
The reason you'll want to subclass DynaActionForm is if you do some
validation you probably will have to override the reset method.
--
Rick Reumann
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------
Do you Yahoo!?
Yahoo! Platinum - Watch CBS' NCAA March Madness, live on your desktop!