title: JSF: why is empty test in rendered invoked during apply request values phase during form submission under request scoped POST REDIRECT GET
body: <p>This question is spawned from the partial answer to <a href="https://stackoverflow.com/questions/9595216/jsf2-why-does-empty-test-in-rendered-of-panelgroup-in-composite-prevent-action/9961045#9961045">JSF2: why does empty test in rendered of panelGroup in composite prevent action from being called?</a></p>
  
  <p>In the following an Element is an @Entity with a name and id. A view.xhtml JSF page takes the id as a viewParam and uses setID(Long id) of the @ManagedBean @RequestScoped ElementController to trigger loading of the corresponding Element by id from database (that plays no further role in the question) and this found Element is set as the 'current' Element available (for historical reasons by a slightly different name) as Element getSelected(). </p>
  
  <p>The view.xhtml page performs a rendered attribute test #{not empty elementController.selected}, and has a h:commandButton with action that performs a faces-redirect, along with the id as query parameter, back to the view.xhtml page.</p>
  
  <p>For some reason I do not fully understand, on form submission the test (and thus getSelected) is invoked in both the apply request phase and the process validations phase, before the viewParam id can be set (and thus before the current/selected Element can be found and set) in the update model values phase.</p>
  
  <p>The greatly abbreviated view.xhtml page is:</p>
  
  <pre><code>&lt;f:view&gt;
   &lt;f:metadata&gt;
      &lt;f:viewParam name="id" value="#{elementController.id}"/&gt;
   &lt;/f:metadata&gt;
  &lt;/f:view&gt;
  &lt;h:body&gt;   
   &lt;h:form&gt;
     &lt;h:panelGroup rendered="#{not empty elementController.selected}"&gt;
         &lt;h:outputText value="#{elementController.selected.name}"/&gt;
     &lt;/h:panelGroup&gt;
  
     &lt;h:commandButton value="Apply" action="#{elementController.action}" /&gt;
  
   &lt;/h:form&gt;
  &lt;/h:body&gt;
  </code></pre>
  
  <p>(The sense of the form submission is lost above, but it does not matter for the this question.)</p>
  
  <p>ElementController extends RequestController:</p>
  
  <pre><code>public void setId(Long id) {
  log_debug("setId","id",id);
  if (id != null) {
      this.id = id;
      T found = (T) getAbstractFacade().find(id);
      if (found == null) {
          String $error = "No object with id(" + id + ") found for class " + getManagedClass().getSimpleName();
          log_error($error);
      }
      setCurrent(found);
   }
  }
  
  public T getSelected() {
    log_debug("getSelected","current",current);        
    if (current == null) {
      log_warn("getSelected","null current Element");
     }
   return current;
  }
  
  public Object action() {
      String $i = "action";
      log_debug($i);
      if (current==null) {
          log_warn($i, "can't generate action outcome for null current element");
          return null;
      }
      return "/view?faces-redirect=true&amp;id="+current.getId();
   }
  </code></pre>
  
  <p>Now on form submission, getSelected() happens to get called twice, and when current==null, once during the apply request values phases and once during the process validations phase, due to the test #{not empty elementController.selected} before the setting of the id (and thus loading of the Element entity) can occur thanks to the viewParam in the view.xhtml.</p>
  
  <p>The question is, why is the rendered=#{not empty elementController.selected} invoked at all during the apply request phase and process validations phase ?</p>
  
  <p>It is not invoked during those phases when I perform an initial GET load of the view.xhtml with id parameter, only during a form submission POST  and subsequent redirect and GET.</p>