Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ajax with CommandButton [duplicate]

Tags:

jsf

jsf-2

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.

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.

So I'm using ajax. Here's the button's, code

<h:commandButton value="Save">
    <f:ajax event="click" listener="#{bean.save}"/>
</h:commandButton>

So far, on the java side, here's the bean's save method

public void save(){
    log.debug("Save executed!");
}

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.

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 <f:param>), something bugs out.

Bottom line: I just need to execute the save method when clicking on the button without refreshing or redirecting anything.

Ideas?

--EDIT--

INDEX.XHTML

<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">
<ui:define name="body">
<h:link outcome="agreementDetail.xhtml" value="EA-15558">
                <f:param name="serviceId" value="EA-15558" />
                <f:param name="site" value="NIC" />
            </h:link>
 </ui:define>

</html>

AgreementDetail.XHTML

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <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">

             <f:view>
                <f:event type="preRenderView" listener="#{agreement.build}"/>
            </f:view>
  <ui:define name="body">
    <f:view>
                    <h:form>
    <h:commandButton value="Save" action="#{agreement.save}">
                            <f:ajax/>
                        </h:commandButton><br/><br/>
    <h:dataTable value="#{agreement.licenseServerNames}" var="licenseServerName">
                            <h:column>
                                <h:inputText value="#{licenseServerName}"/>
                            </h:column>
                        </h:dataTable>
</h:form>
            </f:view>
  </ui:define>        


</html>

AgreementBean.java

@ManagedBean(name="agreement")
@RequestScoped
public class AgreementBean {

@ManagedProperty("#{param.serviceId}")
    private String serviceId;

    @ManagedProperty("#{param.site}")
    private String site;

private List<String> 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);
        }
    }
}
like image 931
Nacho321 Avatar asked Jul 04 '26 06:07

Nacho321


1 Answers

This worked for me."Show" is a boolean that you can set upon successful save.

          <h:commandButton id="ajax" value="Save" action="{agreement.save}" >
                            <f:ajax execute="@form" render="@form" />
              </h:commandButton>
            <h:outputScript rendered="#{agreement.show}">alert("save");</h:outputScript>
like image 191
Justin Cox Avatar answered Jul 08 '26 21:07

Justin Cox