title: JSF 2 basic @RequestScoped CRUD
body: <p>i have these simple pages:</p>
  
  <p>list.xhtml</p>
  
  <pre><code>&lt;h:form id="form"&gt;  
      &lt;h:dataTable value="#{testBean.model}" var="elem"&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;code&lt;/f:facet&gt;
              #{elem.code}
          &lt;/h:column&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;description&lt;/f:facet&gt;
              #{elem.description}
          &lt;/h:column&gt;
          &lt;h:column&gt;
              &lt;f:facet name="header"&gt;action&lt;/f:facet&gt;
              &lt;h:commandButton action="#{testBean.edit(elem)}" value="edit"/&gt;
          &lt;/h:column&gt;
      &lt;/h:dataTable&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>edit.xhtml</p>
  
  <pre><code>&lt;h:form id="form"&gt;
      &lt;h:panelGrid columns="2"&gt;
          &lt;h:outputLabel value="code"/&gt;
          &lt;h:inputText value="#{testBean.selection.code}"/&gt;
  
          &lt;h:outputLabel value="description"/&gt;
          &lt;h:inputText value="#{testBean.selection.description}"/&gt;
      &lt;/h:panelGrid&gt;
  
      &lt;h:commandButton action="#{testBean.update}" value="update"/&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>and this bean:</p>
  
  <pre><code>@ManagedBean
  public class TestBean implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @EJB
      private PersistenceService service;
      private Object selection;
      private List&lt;UnitType&gt; model;
  
      @PostConstruct
      public void init()
      {
          model = service.findAll(UnitType.class);
      }
  
      public String edit(Object object)
      {
          System.out.println(Tracer.current(object));
          setSelection(object);
          return "edit";
      }
  
      public String update()
      {
          System.out.println(Tracer.current(selection));
          return "list";
      }
  
      // getters and setters
  }
  </code></pre>
  
  <p>so the table is rendered, when i click one of the "edit" buttons it navigates to "edit.jsf" showing filled input,
  but when i click the "update" buttons it gives me this error:</p>
  
  <pre><code>javax.el.PropertyNotFoundException: /test2/edit.xhtml @27,54 value="#{testBean.selection.code}": Target Unreachable, 'null' returned null
  </code></pre>
  
  <p>note that i know how to implement a @ViewScoped interface to manage CRUD operations, but this is a simple proof of concept that i need to better understand JSF lifecycle.</p>
  
  <p><strong>so i want "testBean" to be @RequestScoped</strong></p>
  
  <hr>
  
  <p><strong>UPDATE</strong> trying with <code>f:viewParam</code>, still not understanding...</p>
  
  <p>list.xhtml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"&gt;
      &lt;h:head&gt;
          &lt;title&gt;test list&lt;/title&gt;
      &lt;/h:head&gt;
  
      &lt;h:body&gt;
          &lt;h:messages/&gt;
  
          &lt;h:form id="form"&gt;
              &lt;h:dataTable value="#{testBean2.model}" rows="10" var="elem"&gt;
                  &lt;h:column&gt;
                      &lt;f:facet name="header"&gt;converterString&lt;/f:facet&gt;
                      #{elem.converterString}
                  &lt;/h:column&gt;
                  &lt;h:column&gt;
                      &lt;f:facet name="header"&gt;first name&lt;/f:facet&gt;
                      #{elem.firstName}
                  &lt;/h:column&gt;
                  &lt;h:column&gt;
                      &lt;f:facet name="header"&gt;last name&lt;/f:facet&gt;
                      #{elem.lastName}
                  &lt;/h:column&gt;
                  &lt;h:column&gt;
                      &lt;f:facet name="header"&gt;action&lt;/f:facet&gt;
                      &lt;h:commandButton action="#{testBean2.edit}" value="edit"&gt;
                          &lt;f:param name="entity" value="#{elem.converterString}"/&gt;
                      &lt;/h:commandButton&gt;
                      &lt;h:commandButton action="#{testBean2.edit2}" value="edit2"&gt;
                          &lt;f:param name="entity" value="#{elem.converterString}"/&gt;
                      &lt;/h:commandButton&gt;
                  &lt;/h:column&gt;
              &lt;/h:dataTable&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>edit.xhtml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="ISO-8859-1"?&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"&gt;
      &lt;f:metadata&gt;
          &lt;f:viewParam id="entityParam" name="entity" value="#{testBean2.selection}" converter="entityConverter" required="true"/&gt;
      &lt;/f:metadata&gt;
  
      &lt;h:head&gt;
          &lt;title&gt;test edit&lt;/title&gt;
      &lt;/h:head&gt;
  
      &lt;h:body&gt;
          &lt;h:messages/&gt;
  
          &lt;h:form id="form"&gt;
              &lt;h:panelGrid columns="2"&gt;
                  &lt;h:outputLabel value="selection"/&gt;
                  &lt;h:outputText value="#{testBean2.selection.converterString}"/&gt;
  
                  &lt;h:outputLabel value="firstName"/&gt;
                  &lt;h:inputText value="#{testBean2.selection.firstName}"/&gt;
  
                  &lt;h:outputLabel value="lastName"/&gt;
                  &lt;h:inputText value="#{testBean2.selection.lastName}"/&gt;
              &lt;/h:panelGrid&gt;
  
              &lt;h:commandButton action="#{testBean2.update}" value="update" ajax="false"&gt;
                  &lt;f:param name="entity" value="#{testBean2.selection.converterString}"/&gt;
              &lt;/h:commandButton&gt;
          &lt;/h:form&gt;
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>testBean2.java</p>
  
  <pre><code>@ManagedBean
  public class TestBean2 implements Serializable
  {
      private static final long serialVersionUID = 1L;
  
      @EJB
      private PersistenceService service;
  
      private Object selection;
      private List&lt;Person&gt; model;
  
      @PostConstruct
      public void init()
      {
          Tracer.out();
          model = service.queryAll(Person.class);
      }
  
      public String edit()
      {
          JsfUtils.addSuccessMessage("edited");
          return "edit";
      }
  
      public String edit2()
      {
          JsfUtils.addSuccessMessage("edited");
          return "edit?faces-redirect=true&amp;amp;includeViewParams=true";
      }
  
      public void update()
      {
          Tracer.out(selection);
          JsfUtils.addSuccessMessage("updated");
      }
  
      // getters and setters
  }
  </code></pre>
  
  <p>if i press "edit" button it goes to <code>edit</code> page, but selection is null and no message is showed.</p>
  
  <p>if i press "edit2" button it goes to <code>edit</code> page, but selection is null, showing required message and the url is <code>edit.jsf?entity=</code></p>
  
  <p>what am i doing wrong?</p>