Question

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 jstl to separate concerns. Is this possible, and how? I've been googling but I can't seem to find anything.

Was it helpful?

Solution

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)

OTHER TIPS

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".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top