开发者

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

开发者 https://www.devze.com 2023-01-20 20:03 出处:网络
I\'m trying to write a class for this (forEach) tag. Here\'s the class: public class BookList implements Iterable<Book>

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

精彩评论

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