title: Commandlink with ajax works only on second click
body: <p>What I'm trying to do is just a simple logout function. The main page will call a template page; consist of header and content. The header contains the logout button. There will be 2 forms in the page, one in the header (logout button), one in the content (few buttons,links, etc).</p>
  
  <p>My problem is the page refresh when first click on the logout link and only proceed after second click. The problem starts to appear when I include primefaces or ajax code inside the content form. When I remove that particular code, the logout works as intended.</p>
  
  <p>The logout form in main template <code>templateUser.xhtml</code>:</p>
  
  <pre><code>&lt;h:form id="logout"&gt;
      &lt;h:commandLink action="#{tenantController.customLogout(e)}"
          id="logoutBtn" immediate="true" value="Logout" /&gt;
  &lt;/h:form&gt;
  </code></pre>
  
  <p>The backing bean:</p>
  
  <pre><code>public String customLogout(ActionEvent e) {
      return "login.xhtml";
  }
  </code></pre>
  
  <p>FYI: the customLogout method also consist of session destroy, but I just put page redirect here.</p>
  
  <p>In template client, the template is specified as:</p>
  
  <pre><code>&lt;ui:composition template="/templateUser.xhtml"&gt;
  </code></pre>
  
  <p>As for the JSF library, currently use jsf 2.0</p>
  
  <p>Solutions I've tried:</p>
  
  <ul>
  <li><code>immediate="true"</code></li>
  <li>put <code>id</code> for both form and commandLink (template form &amp; content form)</li>
  <li>use primefaces (<code>&lt;p:commandLink&gt;</code> with ajax false ==> this returb to the previous page; the method in backed bean is not running.</li>
  </ul>
  
  <p>Below are few links I tried here:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/2118656/hcommandlink-hcommandbutton-is-not-being-invoked">commandButton/commandLink/ajax action/listener method not invoked or input value not updated</a> </li>
  <li><a href="https://stackoverflow.com/questions/12624159/how-can-i-prevent-page-refresh-on-click-of-hcommandlink">How can I prevent page refresh on click of &lt;h:commandLInk&gt;</a> </li>
  <li><a href="https://stackoverflow.com/questions/4900531/jsf-primefaces-pcommandlink-wont-redirect-to-new-page">JSF PrimeFaces p:commandLink won&#39;t redirect to new page?</a> </li>
  </ul>