开发者

JSTL: can't implement class for tag <c:forEach>

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 ?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜