You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by harish krishnaswamy <wh...@yahoo.com> on 2003/03/16 02:56:33 UTC
Collection-backed form properties solution
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
__________________________________________________
Do you Yahoo!?
Yahoo! Web Hosting - establish your business online
http://webhosting.yahoo.com
---------------------------------------------------------------------
To unsubscribe, e-mail: struts-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: struts-user-help@jakarta.apache.org