title: How to send values to a list in a backing bean and display as a dataTable
body: <p>I'm still learning how to use Facelets and I am trying to send inputted values from a JSF page to a list in a ManagedBean and then display that information in a dataTable. I have JSF page to enter in the data, a backing bean to manage the list, and  a regular application for the normal objects. I can't figure out how to send the input value from the JSF to the backing bean, however.</p>
  
  <p>Here is my view:</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://xmlns.jcp.org/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"&gt;
  
      &lt;h:head&gt;
          &lt;title&gt;Guestbook Form&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;h:form&gt;
          &lt;h1&gt;Guestbook Application Form&lt;/h1&gt;
          &lt;p&gt;Please fill out all entries and click the Submit button&lt;/p&gt;
          &lt;h:panelGrid columns="3"&gt;
              &lt;h:outputText value = "Name: "&gt;&lt;/h:outputText&gt;
              &lt;h:inputText id="nameInputText" required ="true"
                           requiredMessage="Please enter your name"
                           value = "#{guestbookBean.name}"
                           validatorMessage="Name must be fewer than 20 
                           characters"&gt;
                          &lt;f:validateLength maximum="20"/&gt;                             
              &lt;/h:inputText&gt;
              &lt;h:message for="nameInputText" styleClass="error"/&gt;
              &lt;h:outputText value = "Email: "&gt;&lt;/h:outputText&gt;
              &lt;h:inputText id="emailInputText" required ="true"
                           requiredMessage="Please enter your email address"
                           value="#{guestbookBean.email}"
                           validatorMessage="Invalid email address format"&gt;
                  &lt;f:validateRegex pattern ="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"/&gt;                            
              &lt;/h:inputText&gt;
              &lt;h:message for="emailInputText" styleClass="error"/&gt;
              &lt;h:outputText value = "Enter a message: "&gt;&lt;/h:outputText&gt;
              &lt;h:inputTextarea id="messageInputText" required ="true"
                           requiredMessage="Please enter a message"
                           value="#{guestbookBean.message}"
                           validatorMessage="Message must be fewer than 140 characters"&gt;
                          &lt;f:validateLength maximum="140"/&gt;                             
              &lt;/h:inputTextarea&gt;
              &lt;h:message for="messageInputText" styleClass="error"/&gt;
          &lt;/h:panelGrid&gt;
          &lt;h:commandButton value="Submit" type ="Submit" action ="#{guestbookBean.setList()}"/&gt;
          &lt;h:commandButton value ="Reset Form" type ="reset"/&gt;
          &lt;h:outputText escape="false" value ="#{guestbookBean.result}"/&gt;
          &lt;h:dataTable value="#{guestbookBean.list}" var="guests"
                           styleClass="table" cellpadding="5" cellspacing="1" border="2"&gt;
              &lt;h:column&gt;
                  &lt;f:facet name ="header"&gt;Name&lt;/f:facet&gt;
                  #{guests.name}
              &lt;/h:column&gt;
              &lt;h:column&gt;
                  &lt;f:facet name ="header"&gt;Email&lt;/f:facet&gt;
                  #{guests.email}
              &lt;/h:column&gt;
              &lt;h:column&gt;
                  &lt;f:facet name ="header"&gt;Message&lt;/f:facet&gt;
                  #{guests.message}
              &lt;/h:column&gt;
          &lt;/h:dataTable&gt;
          &lt;/h:form&gt;
  
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here is my backing bean:</p>
  
  <pre><code>package guestbook;
  
  import java.util.List;
  import java.util.ArrayList;
  import java.io.Serializable;
  import javax.enterprise.context.Dependent;
  import javax.faces.bean.*;
  
  
  @ManagedBean(name = "guestbookBean")
  public class GuestbookBean implements Serializable{
  
      private String na;
      private String em;
      private String m;
      private List&lt;GuestbookEntry&gt; bookList = new ArrayList&lt;&gt;();
  
      //Set name
       public void setName(String first)
      {
          this.na = first;
      }
  
      //Set email
      public void setEmail(String mail)
      {
          this.em = mail;
      }
  
      //Set message
      public void setMessage(String msg)
      {
          this.m = msg;
      }
  
      //Return the name
      public String getName()
      {
          return na;
      }
  
      //Return the email
      public String getEmail()
      {
          return em;
      }
  
      //Return the message
      public String getMessage()
      {
          return m;
      }
  
  
      public void setList()
      {
         GuestbookEntry bk = new GuestbookEntry();
         bk.setName(na);
         bk.setEmail(em);
         bk.setMessage(m);
  
         bookList.add(0, bk);       
      }
  
      public List&lt;GuestbookEntry&gt; getList()
      {
          return bookList;
      }
  
      // returns result for rendering on the client
     public String getResult()
     {
        if ( !bookList.isEmpty())
        {
           return "&lt;p style=\"background-color:yellow;width:200px;" +
              "padding:5px\"&gt;Data submitted successfully"+ "&lt;/p&gt;";
        }
        else
        {
           return ""; // request has not yet been made
        }
     } // end method getResult
  
  
  }
  </code></pre>
  
  <p>Here is my model:</p>
  
  <pre><code>package guestbook;
  
  public class GuestbookEntry {
  
      private String firstName;
      private String lastName;
      private String email;
      private String message;
  
      public void setFirstName(String fn)
      {
          firstName = fn;
      }
  
      public void setLastName(String ln)
      {
          lastName = ln;
      }
  
      public void setEmail(String em)
      {
          email = em;
      }
  
      public void setMessage(String m)
      {
          message = m;
      }
  
      public String getFirstName()
      {
          return firstName;
      }
  
      public String getLastName()
      {
          return lastName;
      }
  
      public String getEmail()
      {
          return email;
      }
  
      public String getMessage()
      {
          return message;
      }
  
  }
  </code></pre>