views:

22

answers:

2

Hello! 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?

Thanks in advance

+1  A: 

Do you have getter/setter for your Book/BookList class ?

kukudas
No (class implementations above). But i tried to implement getters and setters in the Book class. It haven't helped.
Ivan
+1  A: 

Your BookList class needs to implement Collection to be used with c:forEach.

highlycaffeinated
Thank you! It works! :)
Ivan