Jstl / jsp - iterate over a vector of beans
2 answers
I think what you are looking for is <c: foreach> .
for example, printing the value of the myInt property on instances of MyClass (defined below):
<c:foreach items="${vectors name}" var="pos" >
<!-- print the value of myInt for each position of the array.
Method getMyInt() must exist in pos object.-->
<c:out value="${pos.myInt}"/>
<!-- print the value of myInt for each composed instance.
Method getRelatedInstance() must exist in pos object. -->
<c:out value="${pos.relatedInstance.myInt}"/>
</c:foreach>
where vector name is the name of the vector, for example after execution
Let's assume you have a class called myClass.
public class MyClass{
private MyClass relatedInstance;
//some members and methods
public int getMyInt(){
//return something
}
public MyClass getRelatedInstance(){
return this.relatedInstance;
}
List<myClass> my_vector = getFilledList();
request.setAttribute("vectors name",my_vector)
+1
source to share
Spend using Tom's example, here's something more specific:
<c:foreach items="${myList}" var="myItem">
<c:out value="${myItem.someProperty}"/>
</c:foreach>
Where "myList" is the request attribute that contains your vector.
A common mistake is to forget $ {} around $ {myList} - if you do, JSTL will not throw an error, it will just generate a list for you with one element, the string "myList" ".
0
source to share