jstl/jsp - iterating over a vector of beans

后端 未结 2 869
迷失自我
迷失自我 2021-01-27 19:21

I have a vector of beans that holds information I want to display in my jsp page. I\'m currently just using standard java expressions to display this, I want to look into using

相关标签:
2条回答
  • 2021-01-27 19:46

    To expend on Tom's example, here's something more concrete:

    <c:foreach items="${myList}" var="myItem">
      <c:out value="${myItem.someProperty}"/>
    </c:foreach>
    

    Where "myList" is a request attribute which contains your vector.

    A common error is to forget the ${} around ${myList} - if you do this, JSTL won't throw an error, it'll just generate a list for you with a single item, the string "myList".

    0 讨论(0)
  • 2021-01-27 20:04

    I think that what you are looking for is the < c:foreach > tag.

    for example, printing the value 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 doing a

    Suppose you have a class 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)
    
    0 讨论(0)
提交回复
热议问题