title: Ajax with CommandButton
body: <p>I have a page that has a preRender call that prepares everything to be displayed in the page. I'm not sure if it's relevant, but the page recieves a few params from the index.xhtml that precedes the experience. </p>
  
  <p>I have a commandButton that I need to execute a server-side method (an update, to be precise). There is no need for a refresh on the page. </p>
  
  <p>So I'm using ajax. Here's the button's, code</p>
  
  <pre><code>&lt;h:commandButton value="Save"&gt;
      &lt;f:ajax event="click" listener="#{bean.save}"/&gt;
  &lt;/h:commandButton&gt;
  </code></pre>
  
  <p>So far, on the java side, here's the bean's save method</p>
  
  <pre><code>public void save(){
      log.debug("Save executed!");
  }
  </code></pre>
  
  <p>I've added some logging to check what's being executed. When I click the button, the only thing that happens is that the preRender method is executed (and not entirely, just a part of it). Nothing else happens. Visually, the page is not refreshed or anything. </p>
  
  <p>I suspect that when I click the button, the page is being refreshed and therefore, the preRender method (called Build()) is executed, but since there are no parameters (remember that the Build requires parameters passed through <code>&lt;f:param&gt;</code>), something bugs out.</p>
  
  <p>Bottom line: I just need to execute the save method when clicking on the button without refreshing or redirecting anything. </p>
  
  <p>Ideas?</p>
  
  <p>--EDIT--</p>
  
  <p><strong>INDEX.XHTML</strong></p>
  
  <pre><code>&lt;html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:c="http://java.sun.com/jstl/core"&gt;
  &lt;ui:define name="body"&gt;
  &lt;h:link outcome="agreementDetail.xhtml" value="EA-15558"&gt;
                  &lt;f:param name="serviceId" value="EA-15558" /&gt;
                  &lt;f:param name="site" value="NIC" /&gt;
              &lt;/h:link&gt;
   &lt;/ui:define&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>AgreementDetail.XHTML</strong></p>
  
  <pre><code>    &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
      &lt;html xmlns="http://www.w3.org/1999/xhtml"
            xmlns:ui="http://java.sun.com/jsf/facelets"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:c="http://java.sun.com/jstl/core"&gt;
  
               &lt;f:view&gt;
                  &lt;f:event type="preRenderView" listener="#{agreement.build}"/&gt;
              &lt;/f:view&gt;
    &lt;ui:define name="body"&gt;
      &lt;f:view&gt;
                      &lt;h:form&gt;
      &lt;h:commandButton value="Save" action="#{agreement.save}"&gt;
                              &lt;f:ajax/&gt;
                          &lt;/h:commandButton&gt;&lt;br/&gt;&lt;br/&gt;
      &lt;h:dataTable value="#{agreement.licenseServerNames}" var="licenseServerName"&gt;
                              &lt;h:column&gt;
                                  &lt;h:inputText value="#{licenseServerName}"/&gt;
                              &lt;/h:column&gt;
                          &lt;/h:dataTable&gt;
  &lt;/h:form&gt;
              &lt;/f:view&gt;
    &lt;/ui:define&gt;        
  
  
  &lt;/html&gt;
  </code></pre>
  
  <p><strong>AgreementBean.java</strong></p>
  
  <pre><code>@ManagedBean(name="agreement")
  @RequestScoped
  public class AgreementBean {
  
  @ManagedProperty("#{param.serviceId}")
      private String serviceId;
  
      @ManagedProperty("#{param.site}")
      private String site;
  
  private List&lt;String&gt; licenseServerNames; //GETTERS AND SETTERS OMITTED TO AVOID EXCESS CODE
  
  @PostConstruct
      public void build(){
          logger.debug("START");
          methodOne();    
                  logger.debug("END");        
      }
  
  public void save(){
          logger.debug("SAVE!!!!!");
          for(String name : licenseServerNames){
              logger.debug("Servername = "+name);
          }
      }
  }
  </code></pre>