0

I've been strugling with an issue I can't find the way to solve it and I can't find anything related on the web.

I'm using Netbeans with Primefaces to develop a JSF web app.

Everything works fine except that sometimes the call to the actionlisteners is being done several times.

For example this is my JSF page with primefaces:

<h:body>
   <ui:composition template="home.xhtml">

        <ui:define name="content">
            <h2>Administración de alertas SMS</h2>


                    <p:panel id="display">

                        <p:selectBooleanCheckbox itemLabel="Enviar alertas SMS" value="#{smsConfigBean.alertsEnabled}"/>

                        <p>
                        <h:outputText value="Mensaje de Texto: " /><br /><br />
                        <p:inputTextarea rows="5" cols="50" counterTemplate="{0} caracteres restantes." counter="counter" maxlength="160" value="#{smsConfigBean.smsMessage}" id="smsMessage"/>
                        <br />
                        <h:outputText id="counter" />
                        </p>

                        <p>   
                        <h:outputText value="Dispositivo de envio SMS: " />  
                        <p:selectOneRadio id="options" value="#{smsConfigBean.usePhone}">  
                            <f:selectItem itemLabel="Modem USB"  itemValue="false" />  
                            <f:selectItem itemLabel="Telefono Android" itemValue="true" />                                        
                        </p:selectOneRadio>  
                        </p>
                        <br />
                        <p:tabView id="tabView">  

                            <p:tab id="tab1" title="Modem USB">  
                                <h:panelGrid id="modemGrid" columns="2" cellpadding="4">  
                                    <h:outputText value="Puerto: " />  
                                    <p:inputText id="port" value="#{smsConfigBean.port}"/>  

                                    <h:outputText value="Bit Rate (Opcional): " />  
                                    <p:inputText id="bitRate" value="#{smsConfigBean.bitRate}"/>  

                                    <h:outputText value="Nombre de modem (Opcional): " />  
                                    <p:inputText id="modemName" value="#{smsConfigBean.modemName}"/>

                                    <h:outputText value="PIN: " />  
                                    <p:inputText id="pin" value="#{smsConfigBean.modemPin}"/>

                                    <h:outputText value="Centro de Mensajes: " />  
                                    <p:inputText id="smsc" value="#{smsConfigBean.SMSC}"/>                                  
                                </h:panelGrid>
                            </p:tab>  

                            <p:tab id="tab2" title="Telefono Android">  

                                 <h:panelGrid id="phoneGrid" columns="2" cellpadding="4">  
                                    <h:outputText value="Path ADB:" />  
                                    <p:inputText id="adbPath" value="#{smsConfigBean.adbPath}"/>  

                                    <h:outputText value="Directorio de Configuracion:" />  
                                    <p:inputText id="configPath" value="#{smsConfigBean.configPath}"/>  

                                    <h:outputText value="Modo de conexion " />  
                                    <p:selectOneRadio id="connectOptions" value="#{smsConfigBean.useWifi}">  
                                        <f:selectItem itemLabel="USB" itemValue="false"/>  
                                        <f:selectItem itemLabel="WiFi" itemValue="true" />                                        
                                    </p:selectOneRadio>  

                                    <h:outputText value="Direccion IP (Solo para WiFi): " />  
                                    <p:inputText id="ipDir" value="#{smsConfigBean.ipDir}"/>  

                                </h:panelGrid> 
                            </p:tab>  

                        </p:tabView> 


                    </p:panel>

                    <br />
                    <p:commandButton id="saveSmsAlerts" value="Guardar" update="messages" action="#{smsConfigBean.saveChanges}" actionListener="#{smsConfigBean.saveChanges}"/>



        </ui:define>

    </ui:composition>
</h:body>

And this is my backend bean:

package Beans;

import helpers.Global; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped;

@ManagedBean @SessionScoped public class SmsConfigBean {

private Boolean alertsEnabled;
private Boolean usePhone;
private Boolean useWifi;

private String port;
private int bitRate;
private String modemName;
private String modemPin;
private String SMSC;
private String adbPath;
private String configPath;
private String ipDir;

private String smsMessage;

public SmsConfigBean() {
    alertsEnabled = Global.getAlertsEnabled();      
    port = Global.getPort();
    bitRate = Global.getBitRate();
    modemName = Global.getModemName();
    modemPin = Global.getModemPin();
    SMSC = Global.getSMSC();
    usePhone = Global.getUsePhone();
    adbPath = Global.getAdbPath();
    configPath = Global.getConfigPath();
    useWifi = Global.getUseWifi();
    ipDir = Global.getIpDir();     
    smsMessage = Global.getSmsMessage();
}

public Boolean getAlertsEnabled() {
    return alertsEnabled;
}

public void setAlertsEnabled(Boolean alertsEnabled) {
    this.alertsEnabled = alertsEnabled;
}

public String getPort() {
    return port;
}

public void setPort(String port) {
    this.port = port;
}

public int getBitRate() {
    return bitRate;
}

public void setBitRate(int bitRate) {
    this.bitRate = bitRate;
}

public String getModemName() {
    return modemName;
}

public void setModemName(String modemName) {
    this.modemName = modemName;
}

public String getModemPin() {
    return modemPin;
}

public void setModemPin(String modemPin) {
    this.modemPin = modemPin;
}

public String getSMSC() {
    return SMSC;
}

public void setSMSC(String SMSC) {
    this.SMSC = SMSC;
}

public Boolean getUsePhone() {
    return usePhone;
}

public void setUsePhone(Boolean usePhone) {
    this.usePhone = usePhone;
}

public String getAdbPath() {
    return adbPath;
}

public void setAdbPath(String adbPath) {
    this.adbPath = adbPath;
}

public String getConfigPath() {
    return configPath;
}

public void setConfigPath(String configPath) {
    this.configPath = configPath;
}

public Boolean getUseWifi() {
    return useWifi;
}

public void setUseWifi(Boolean useWifi) {
    this.useWifi = useWifi;
}

public String getIpDir() {
    return ipDir;
}

public void setIpDir(String ipDir) {
    this.ipDir = ipDir;
}

public String getSmsMessage() {
    return smsMessage;
}

public void setSmsMessage(String smsMessage) {
    this.smsMessage = smsMessage;
}

public void saveChanges(){
    Global.setSmsMessage(smsMessage);
    Global.setIpDir(ipDir);
    Global.setUseWifi(useWifi);
    Global.setConfigPath(configPath);
    Global.setAdbPath(adbPath);
    Global.setUsePhone(usePhone);
    Global.setSMSC(SMSC);
    Global.setModemPin(modemPin);
    Global.setModemName(modemName);
    Global.setBitRate(bitRate);
    Global.setPort(port);
    Global.setAlertsEnabled(alertsEnabled);
    Global.sendInfoResponseMessage("Cambios guardados con exito");
} 

}

The problem is that when saving the changes of this form by hitting the commandButton 'saveSmsAlerts' the call is being made twice. Resulting in the growl message to be shown twice too. (You won't see the growl component because it was defined in the template)

This happens also in another page that I did for the same app that uploads a file using fileuploader. The file is uploaded 4 times!!

I'm using chrome to test the application and netbeans to develop it.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
martin
  • 64
  • 1
  • 7
  • Can you remove the action attribute since you are not returning an outcome. actionListener alone is sufficient. Also change the method to public void saveChanges(ActionEvent e) – Ravi Kadaboina Sep 10 '12 at 03:28

1 Answers1

2

You should remove one of action or actionListener attribute.

<p:commandButton id="saveSmsAlerts" value="Guardar" update="messages" action="#{smsConfigBean.saveChanges}"/>

if you remove action attribute you should change the action method signature

<p:commandButton id="saveSmsAlerts" value="Guardar" update="messages"  actionListener="#{smsConfigBean.saveChanges}"/>

Like this

public void saveChanges(ActionEvent e){
...
}

here is the difference between action and actionListener

Differences between action and actionListener

Community
  • 1
  • 1
erencan
  • 3,725
  • 5
  • 32
  • 50
  • An `ActionEvent` is not necessary in the method being called. Also the OP should be using `action` in this case not `actionListener` to see the difference between the two see [this question](http://www.stackoverflow.com/questions/3909267/). – siebz0r Sep 10 '12 at 09:24