I'm trying to write a class for this (forEach) tag.
Here's the class:
public class BookList implements Iterable<Book>
{
public ArrayList<Book> book_list;
public BookList()
{
book_list = new ArrayList<Book>(2);
book_list.add(new Book("BookTitle_01","book_01.html"));
book_list.add(new Book("BookTitle_02","book_02.html"));
}
@Override
public Iterator<Book> iterator() {
return book_list.iterator();
}
}
Book class:
public class Book {
public String title;
public String url;
public Book(String new_title, String new_url)
{
title = new_title;
url = new_url;
}
}
part of JSP page:
<jsp:useBean id="books" scope="session" class="vanya.BookList" />
<c:if test="${inputedFlag}">
开发者_开发问答 <ul>
<c:forEach var="book" items="${books}">
<li>
<a href="${book.url}">${book.title}</a>
</li>
</c:forEach>
</ul>
</c:if>
But it doesn't work... What is wrong with my code?
Your BookList class needs to implement Collection to be used with c:forEach.
Do you have getter/setter for your Book/BookList class ?
精彩评论