handling dynamic html objects in struts with form beans
I want to know how to create dynamic struts html form controls
for example, <% for (int k=1; k<=10; k++) { %> <html:select property="salesItemsId<%=k%>"> <html:option value="0">Select</html:option>
<html:options collection="itemsList" property="iteCode" labelProperty="iteItem"/> </html:select> <% } %> my question here is,
if I put the loop and get the values like, property="salesItemsId<%=k%>....
it is not allowed itseems...nothing printed on the screen...
Is there anyother way to do this?
[592 byte] By [
loguKKa] at [2007-11-27 6:12:06]

# 1
I don't think there is a straight fwd solution to this even though you use DynaActionForm object you got to configure its property statically in struts-config.xml.
i sort of feel the thing which you are trying to achieve could be done by using the other way around too..
say you have a property called a "collectionList" which returns a collection object like an java.util.ArrayList you can go about using
the below mechanism instead of creating a dynamic property.
<logic:iterate id="element" property="collectionList">
<html:select name="element" property="dropdownvalue">
<html:option value="0">Select</html:option>
<html:options collection="itemsList" property="iteCode" labelProperty="iteItem"/>
</html:select>
</logic:iterate>
and here is you struts-config.xml
<form-bean name="SampleForm" type="org.apache.struts.action.DynaActionForm">
<form-property name="collectionList" type="java.util.ArrayList"/>
<form-property name="iteCode" type="java.util.ArrayList"/>
-
</form-bean>
Action Class
==========
DynaActionForm dform = (DynaActionForm) form;
ArrayList DdSelectedvalues = (ArrayList) dform.get(collectionList);
Hope this might work :)
REGARDS,
RaHuL
# 2
Hi..Rahul Thanks for the reply.... i think in this same idea..i have already tried to generated the form selection controls.. even this idea also will work ....
my actualy problem is when i try to do some client side process (with AJAX)....
i could not get the each <select control name...as i can get the values in the server side array index.....
how to do get the individual ><select name...>
# 3
well as you knw the string associate with property would be the name for
<select control id="propertyName"> <=> <html:select property="propertyName" ....>
by which you access for request parameters.
as the name is unique among set of combo boxes you might have to use request.getParameterValues("propertyName").
checkout the below article which i think would be one of the best articles to use AJAX via struts.
http://today.java.net/pub/a/today/2005/10/27/sprinkle-ajax-magic-into-struts-webapp.html
or the below post
http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=58&t=007567
or try using
http://struts.sourceforge.net/ajaxtags/index.html
http://www.backbase.com/#home/products/editions/struts_edition.xml (which might help you to an extent)
REGARDS,
RaHuL