tags:

views:

837

answers:

2

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.

A: 

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)
Tom
This seems to be fine for primitives like strings, but I am having trouble working with collections of beans which hold various objects.
That's excellent thanks, I was getting a little confused with el requiring classes, and my jstl libs were playing up (umpteen different tabs telling me all different ways of deploying them).Many thanks!
A: 

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

skaffman
Thanks, i´ve edited some mistakes in my answer.
Tom