title: How do I display a list of items from a Bean onto a JSF webpage?
body: <p>I'm new to JSF and in the process of learning im building an online book store application. </p>
  
  <p>I have 1 class and 1 bean: <code>Book.java</code> and <code>BookCatelogBean.java</code>. The Book class has 3 properties: <code>id</code>, <code>title</code>, and <code>author</code> with it's corresponding getters and setters. The <code>BookCatelogBean</code> contains an <code>ArrayList&lt;Book&gt;</code> where I populate it with <code>Books</code> (in future I will connect it to a database). </p>
  
  <p>I have two pages: <code>index.xhtml</code> and <code>book.xhtml</code>. I want to display the list of book titles on <code>index.xhtml</code> each formatted as a REST link and their ID to <code>book.xhtml</code>, like so: <code>&lt;h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" /&gt;</code></p>
  
  <p>I know how to use <code>BookCatelogBean</code> to display 1 <code>book</code> but I want to display all of them? I have an idea to call a method from <code>BookCatelogBean</code> called <code>getAllBooks()</code> that returns each of the books titles but how would I return each one of them to index.xhtml as a JavaserverFace link instead of a string?</p>
  
  <p>Thanks</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Book.java</strong></p>
  
  <pre><code>package bookshop;
  
  import java.io.Serializable;
  
  public class Book implements Serializable {
  
      private int id;
      private String title;
      private String author;
  
      public Book(int id, String title, String author){
          this.title = title;
          this.id = id;
          this.author = author;
      }
  
      public String getAuthor() {
          return author;
      }
  
      public void setAuthor(String author) {
          this.author = author;
      }
  
      public int getId() {
          return id;
      }
  
      public void setId(int id) {
          this.id = id;
      }
  
      public String getTitle() {
          return title;
      }
  
      public void setTitle(String title) {
          this.title = title;
      }
  }
  </code></pre>
  
  <p><strong>BookCatelogBean.java</strong></p>
  
  <pre><code>package bookshop;
  
  import java.io.Serializable;
  import java.util.ArrayList;
  import java.util.Arrays;
  import javax.faces.bean.ManagedBean;
  import javax.faces.bean.SessionScoped;
  
  @ManagedBean
  @SessionScoped
  public class BookCatelogBean implements Serializable {
      private int currentItem = 0;
  
      private ArrayList&lt;Book&gt; books = new ArrayList&lt;Book&gt;(Arrays.asList(
              new Book(1, "Theory of Money and Credit", "Ludwig von Mises"),
              new Book(2, "Man, Economy and State", "Murry Rothbard"),
              new Book(3, "Real Time Relationships", "Stefan Molyneux")));
  
      public String getTitle(){
          return books.get(currentItem).getTitle();
      }
  
      public int getId(){
          return books.get(currentItem).getId();
      }
  
      public String getAuthor(){
          return books.get(currentItem).getAuthor();
      }
  
  }
  </code></pre>
  
  <p><strong>index.xhtml</strong></p>
  
  <pre><code>&lt;?xml version='1.0' encoding='UTF-8' ?&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:h="http://java.sun.com/jsf/html"&gt;
      &lt;h:head&gt;
          &lt;title&gt;BookShop&lt;/title&gt;
  
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:link outcome="book?id=#{bookCatelogBean.id}" value="#{bookCatelogBean.title}" /&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>