title: JSF2.1 different behavior ajax listener used in page with facelets or without facelets
body: <p>If a commandButton with ajax support used in een .xhtml without facelets templates, the ajax listener has to be an attribute of the commandButton-tag. If used in conjunction with a template, the listener has to be an attribute of the ajax-tag. <br />I needed some time to figure this out so if this is correct and a normal behavior, be warned. <br /></p>
  
  <p>The viewscoped bean</p>
  
  <pre><code>@ManagedBean
  @ViewScoped
  public class Test implements Serializable {
      private static final long serialVersionUID = 123456L;
      private static int i = 0;
      private int counter;
      private String test = "test ";
  
      @PostConstruct
      public void test() {
          System.out.println(".......... PostConstruct");
          i++;
      }
  
      public String getTest() {
          return test + i;
      }
  
      public String action() {
          counter++;
          System.out.println(".......... action() " + counter);
          return "";
      }
  
      public void listener() {
          counter++;
          System.out.println(".......... listener() " + counter);
      }
  
      public void ajaxListener(AjaxBehaviorEvent actionEvent) {
          System.out.println(".......... ajaxListener() - " + 
                             "AjaxBehaviorEvent: " + actionEvent);
      }
  
      public void ajax() {
          System.out.println(".......... ajax() - not using Facelets " + 
                             counter);
      }
  
      public void ajaxList2(ActionEvent actionEvent) {
          System.out.println(".......... ajaxList2() - ActionEvent" + 
                             actionEvent);
      }
  
      public int getCounter() {
          return counter;
      }
  }
  </code></pre>
  
  <p>the first 2 buttons work in both pages (with and without using facelets). The other buttons don't see the buttons value attribute.</p>
  
  <p>the .xhtml using facelets</p>
  
  <pre><code>&lt;ui:composition  xmlns="http://www.w3.org/1999/xhtml"
               xmlns:h="http://java.sun.com/jsf/html"
               xmlns:f="http://java.sun.com/jsf/core"
               xmlns:ui="http://java.sun.com/jsf/facelets"
               xmlns:p="http://primefaces.prime.com.tr/ui"
               template="/WEB-INF/templates/test.xhtml"&gt;
  &lt;ui:define name="ui_content"&gt;
  
      #{test.test}
  
      &lt;h:form id="frm"&gt;
          &lt;h:commandButton value="testAction" action="#{test.action}"/&gt;
          &lt;h:commandButton value="testListener" actionListener="#{test.listener}"/&gt;
          &lt;h:commandButton value="Ajax - works ONLY using Facelets" immediate="true"  &gt;
              &lt;f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" /&gt;
          &lt;/h:commandButton&gt;
          &lt;h:commandButton value="testAjax - works ONLY without using Facelets" immediate="true" action="#{test.ajax}" &gt;
              &lt;f:ajax event="click" execute="@form" render=":grp2"/&gt;
          &lt;/h:commandButton&gt;
          &lt;h:commandButton value="testAjax - actionListener works ONLY without using Facelets"
                           immediate="true" actionListener="#{test.ajaxList2}" &gt;
              &lt;f:ajax event="click" execute="@form" render=":grp2"/&gt;
          &lt;/h:commandButton&gt;
      &lt;/h:form&gt;
      &lt;h:panelGroup id="grp1" rendered="#{test.counter &gt; 2}"&gt;
          1st group: #{test.counter}
      &lt;/h:panelGroup&gt;&lt;br /&gt;
      &lt;h:panelGroup id="grp2" rendered="#{test.counter &gt; 7}"&gt;
          2de group: #{test.counter}
      &lt;/h:panelGroup&gt;
  </code></pre>
  
  <p></p>
  
  <p>the .xhtml without facelets</p>
  
  <pre><code>&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"
               xmlns:p="http://primefaces.prime.com.tr/ui"&gt;
  
      #{test.test}
  
      &lt;h:form id="frm"&gt;
          &lt;h:commandButton value="testAction" action="#{test.action}"/&gt;
          &lt;h:commandButton value="testListener" actionListener="#{test.listener}"/&gt;
          &lt;h:commandButton value="Ajax - works ONLY using Facelets" immediate="true"  &gt;
              &lt;f:ajax event="click" execute="@form" render=":grp2" listener="#{test.ajaxListener}" /&gt;
          &lt;/h:commandButton&gt;
          &lt;h:commandButton value="testAjax - action works ONLY without using Facelets" immediate="true" action="#{test.ajax}" &gt;
              &lt;f:ajax event="click" execute="@form" render=":grp2"/&gt;
          &lt;/h:commandButton&gt;
          &lt;h:commandButton value="testAjax - actionListener works ONLY without using Facelets" immediate="true" actionListener="#{test.ajaxList2}" &gt;
              &lt;f:ajax event="click" execute="@form" render=":grp2"/&gt;
          &lt;/h:commandButton&gt;
      &lt;/h:form&gt;
      &lt;h:panelGroup id="grp1" rendered="#{test.counter &gt; 2}"&gt;
          1st group: #{test.counter}
      &lt;/h:panelGroup&gt;&lt;br /&gt;
      &lt;h:panelGroup id="grp2" rendered="#{test.counter &gt; 7}"&gt;
          2nd group: #{test.counter}
      &lt;/h:panelGroup&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>the template file</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:ui="http://java.sun.com/jsf/facelets"
        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&lt;/title&gt;
      &lt;/h:head&gt;
      &lt;h:body&gt;
          &lt;f:view&gt;
              &lt;div id="page"&gt;
                  &lt;ui:insert name="ui_header"&gt;header&lt;/ui:insert&gt;
                  &lt;ui:insert name="ui_subnav"/&gt;
                  &lt;div id="content"&gt;
                      &lt;ui:insert name="ui_content"/&gt;
                  &lt;/div&gt;
                  &lt;ui:insert name="ui_footer" /&gt;
              &lt;/div&gt;
          &lt;/f:view&gt;      
      &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>