Your solution requires JSP scriplet code. I need to do it using the Struts taglib. Struts removes all scriplet code from the JSP and conforms to the MVC architecture.
Main Topics
Browse All TopicsI have a logic:iterate block in my JSP file that needs to iterate over two Collections (ArrayList) simultaneously. That is:
<logic:iterate id="products" name="displayProductsForm"
<LI><a href="http://www.mydomain.
</logic:iterate>
Problem is, the "property" attribute in the bean:write tag is in error (I put it in to try to describe what I am trying to do.) In other words, my ActionForm class has two ArrayLists (productName and productId) and I want to put a value from each in this single HTML line. As you can see, the user will see a product name link which, when clicked on, will take them to a product description page passing the product ID to a cgi script.
How do I code the iterate block? I can iterate over one property, but not two. As you're no doubt aware, I'm pretty new to Struts.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I didn't have much luck with indexId.
I ran across this code which appears to do just what I want - accessing two Collections (ArrayList productId, productName) within a single iteration loop:
<logic:iterate id="products" name="productsForm">
<TD><bean:write name="products" property="productId" /></TD>
<TD><bean:write name="products" property="productName" /></TD>
</logic:iterate>
I'm assuming there is a Bean named ProductsForm.class. What is the relevance of the 'id' parameter? Is is just a local variable for the use of bean:write, or is it a Bean that feeds individual Collection elements to this page?
I'm also assuming a struts-config.xml entry as follows:
<form-beans>
<form-bean name="productsForm"
type="com.bnb.ProductsForm
</form-beans>
<action-mappings>
<action
path="/displayProducts"
type="com.bnb.ProductsActi
name="productsForm"
input="/pages/error.jsp"
scope="session">
<forward name="success" path="/pages/productSummar
<forward name="failure" path="/pages/error.jsp" />
</action>
</action-mappings>
How far off-base am I?
> accessing two Collections (ArrayList productId, productName) within a single iteration loop:
Thats not what that code does. It is iterating over *one* list, and accessing two *properties for the elements.
> What is the relevance of the 'id' parameter?
It's the name of the variable used to hold the list elemement.
Back to using indexed properties, the following code snippet:
<logic:iterate id="products" indexId = "i" name="productsForm" property="productId" >
<LI><a href="http://www.bnb.com/m
property="productId[i]" filter="true" />"> <bean:write name="products"
property="sizes[i]" filter="true"/></a>
</LI>
</logic:iterate>
...results in:
java.lang.IllegalArgumentE
The ActionForm includes:
...
private ArrayList productId;
private ArrayList sizes;
...
public String getProductId(int i) {
return (String)(productId.get(i))
}
public String getSizes(int i) {
return (String)(sizes.get(i));
}
...
OK. This code works insofar as it properly iterates over the productIds and inserts them properly into the JSP:
<logic:iterate id="products" name="productsForm" property="productId" >
<LI><a href="http://www.bnb.com/m
</LI>
</logic:iterate>
ActionForm code:
private ArrayList productId;
public ArrayList getProductId() {
return productId;
}
try something like:
<logic:iterate id="products" name="productsForm" indexId="i" property="productId" >
<LI><a href="http://www.bnb.com/m
</LI>
</logic:iterate>
The code you suggested results in the following compile error:
org.apache.jasper.JasperEx
cannot resolve symbol
symbol : variable productsForm
location: class org.apache.jsp.pages.produ
out.print( productsForm.getSizes(i) );
^
'productsForm' is the Bean name referenced in the struts-config.xml file (see above.)
I then tried changing 'productsForm' (the Bean name) to ProductsForm (the Bean class) - no luck.
I then added a 'jsp:useBean' statement - still, no luck.
snazzyrags, if you use 2 collections, you can avoid using index and thus you cannot avoid using scriptlet.
But, there is a more elegant solution is to define a product class in your formbean:
public class ProductsForm ... {
private List productId;
private List productName;
...
public static class Product {
private String id;
private String name;
public Product( String id, String name ) { this.id=id; this.name=name; }
public String getId() { return id; }
public void setId( String id ) { this.id = id; }
public String getName() { return name; }
public void setName( String name ) { this.name = name; }
}
public List getProducts {
int size = productId.size();
List products = new ArrayList( size );
for( int ii = 0; ii < size; ii ++ ) {
products.add( new Product( productId.get(ii), productName.get(ii) ) );
}
return products;
}
...
}
then you can easily do this:
<logic:iterate id="product" name="displayProductsForm"
<LI><a href="http://www.mydomain.
</logic:iterate>
In my Struts application I have an Order object which has an property which is Array of OrderItem object .This OrderItem Object has an property AvaillableStockQuantity which is showing the inventory stock . Now in Jsp page I am Iteraring through every Order objrct and 2nd iterator iterating through each orderItem object and I have populated AvaillableStockQuantity in combobox .Now I want to get the value of the selected Quantity in the onchange event of combobox but I failed to get it. pls help
Business Accounts
Answer for Membership
by: objectsPosted on 2004-02-26 at 15:41:48ID: 10465284
Assuming both lists have same number of elements you could use:
<%
Iterator i = list1.iterator();
Iterator j = list2.iterator();
while (i.hasNext())
{
Type1 o1 = (Type1) i.next();
Type2 o2 = (Type2) j.next();
...