2009-05-03 69 views
0

我有一個bean的向量,它擁有我想要顯示在我的jsp頁面中的信息。我目前只是使用標準的Java表達式來顯示這個,我想研究一下使用jstl來分離問題。這是可能的,以及如何?我一直在谷歌搜索,但我似乎無法找到任何東西。jstl/jsp - 迭代遍歷一個向量的bean

回答

1

我認爲你正在尋找的是< C:的foreach>標籤。

例如,印刷在MyClass的實例值敏屬性(定義如下):

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

其中矢量名是載體的名稱,例如,做一個

後假設你有一個類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

對於像字符串這樣的原語來說,這似乎很好,但我在處理容納各種對象的bean集合時遇到了問題。 – user70835 2009-05-03 21:52:55

+0

非常感謝,我對el需要類有點困惑,而我的jstl庫正在打開(很多不同的選項卡告訴我部署它們的所有不同方式)。 非常感謝! – user70835 2009-05-03 22:27:30

0

要花費上湯姆的例子,這裏有更具體的東西:

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

其中「myList中」是包含您的載體請求屬性。

一個常見的錯誤是忘記$ {myList}周圍的$ {} - 如果你這樣做,JSTL將不會拋出錯誤,它只會爲你生成一個單一項目的列表,字符串「我的列表」。