简体   繁体   中英

Command button dont fire action - Primefaces and JSF

Hello i have look a lot of tutorials and post but y can't see the problem. I have a dialog that have to save data into the database calling a method in a managed bean, but when i debug the method dont fire. My code of the page is:

<h:form>
        <p:commandButton id="btnCreateCity" update=":formCreateCity" oncomplete="createCity.show()"
                         title="Crear nueva ciudad" value="Crear ciudad" icon="ui-icon-search"/>
</h:form>
<h:form id="formCreateCity">
        <p:dialog header="Crear ciudad" widgetVar="createCity" id="dlgCreateCity"
                  resizable="false" modal="true" showEffect="fade" hideEffect="explode">
            <!-- Alta de ciudades -->
            <f:facet name="header">
            <h:outputLabel value="Agregar nueva ciudad"/>
            </f:facet>
            <p>
                <h:outputLabel for="city" value="Nombre de la ciudad:"/>
                <p:inputText id="city" value="#{locationController.cityName}"/>
                <p:message for="city"/>
            </p>
            <p>
                <h:outputLabel for="postCode" value="Código Postal:"/>
                <p:inputText id="postCode" value="#{locationController.postCode}"/>
                <p:message for="postCode"/>
            </p>
            <p>
                <p:selectOneMenu id="countries" value="#{locationController.selectedCountry}" required="true">  
                    <f:selectItem itemLabel="Seleccionar uno" itemValue="" />  
                    <f:selectItems value="#{locationController.countries}"
                        var="country"
                        itemLabel="#{country.name}"
                        itemValue="#{country.idCountry}"/>
                </p:selectOneMenu> 
            </p>
            <f:facet name="footer">
                <p:commandButton value="Aceptar" 
                                 actionListener="#{locationController.saveCity}" 
                                 oncomplete="createCity.hide()" 
                                 icon="ui-icon-save">
                    <p:ajax update=":mainForm"/>
                </p:commandButton> 
                <p:commandButton value="Cancelar" oncomplete="createCity.hide()" 
                                 icon="ui-icon-cancel"/> 
            </f:facet>
        </p:dialog>
    </h:form>

And my method in the managed bean is:

public void saveCity(){
    cityService = new CityServiceImpl();
    boolean saved = cityService.save(cityName, postCode, selectedCountry);
    if(saved){
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage("mainForm", new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Operación realizada", 
                "Se guardó la ciudad"));
    }else{
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage("mainForm", new FacesMessage(
                FacesMessage.SEVERITY_INFO, "Operación no realizada", 
                "No se pudo guardar la ciudad"));
    }

}

I tried putting javax.faces.event.ActionEvent in a parameter of the method and nthing. Can you help me?

add process="@this" with command button

<p:commandButton value="Aceptar" process="@this"
                             actionListener="#{locationController.saveCity}" 
                             oncomplete="createCity.hide()" 
                             icon="ui-icon-save">

Try using this:

<p:commandButton value="Aceptar" 
    action="#{locationController.saveCity}" 
    oncomplete="createCity.hide()" 
    icon="ui-icon-save" 
    update=":mainForm"
 />

I couldn't find the mainForm in the code you posted. If the mainForm is not present in the View you will get an exception. If you are trying to update the formCreateCity just use update="@form" instead.

Here are the code I used to test it:

The View

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <p:messages autoUpdate="true" showDetail="true" showSummary="true" />

        <h:form id="mainForm">

        </h:form>
        <h:form>
            <p:commandButton id="btnCreateCity" update=":formCreateCity" oncomplete="createCity.show()"
                             title="Crear nueva ciudad" value="Crear ciudad" icon="ui-icon-search"/>
        </h:form>
        <h:form id="formCreateCity">
            <p:dialog header="Crear ciudad" widgetVar="createCity" id="dlgCreateCity"
                      resizable="false" modal="true" showEffect="fade" hideEffect="explode">
                <!-- Alta de ciudades -->
                <f:facet name="header">
                    <h:outputLabel value="Agregar nueva ciudad"/>
                </f:facet>
                <p>
                    <h:outputLabel for="city" value="Nombre de la ciudad:"/>
                    <p:inputText id="city" value="#{locationController.cityName}"/>
                    <p:message for="city"/>
                </p>
                <p>
                    <h:outputLabel for="postCode" value="Código Postal:"/>
                    <p:inputText id="postCode" value="#{locationController.postCode}"/>
                    <p:message for="postCode"/>
                </p>
                <p>
                    <p:selectOneMenu id="countries" value="#{locationController.selectedCountry}" required="true">  
                        <f:selectItem itemLabel="Seleccionar uno" itemValue="" />  
                        <f:selectItems value="#{locationController.countries}"
                                       var="country"
                                       itemLabel="#{country.name}"
                                       itemValue="#{country.idCountry}"/>
                    </p:selectOneMenu> 
                </p>
                <f:facet name="footer">
                    <p:commandButton value="Aceptar" 
                                     actionListener="#{locationController.saveCity}" 
                                     oncomplete="createCity.hide()" 
                                     icon="ui-icon-save" />
                    <p:commandButton value="Cancelar" oncomplete="createCity.hide()" 
                                     icon="ui-icon-cancel"/> 
                </f:facet>
            </p:dialog>
        </h:form>
    </h:body>
</html>

The Entity

import java.util.Objects;

/**
 *
 * @author DIRGTI
 */
public class Country {

    private Long idCountry;
    private String name;

    public Country(){

    }

    public Country(Long idCountry, String name) {
        this.idCountry = idCountry;
        this.name = name;
    }

    public Long getIdCountry() {
        return idCountry;
    }

    public void setIdCountry(Long idCountry) {
        this.idCountry = idCountry;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 89 * hash + Objects.hashCode(this.idCountry);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Country other = (Country) obj;
        if (!Objects.equals(this.idCountry, other.idCountry)) {
            return false;
        }
        return true;
    }
}

The Converter

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

/**
 *
 * @author DIRGTI
 */
@FacesConverter(forClass = Country.class)
public class CountryConverter implements Converter {

    @Override
    public Object getAsObject(FacesContext context, UIComponent component, String value) {
        return new Country(1L, "Brasil");
    }

    @Override
    public String getAsString(FacesContext context, UIComponent component, Object value) {
        if (value == null) {
            return null;
        }
        return ((Country) value).getIdCountry().toString();
    }
}

The Managed Bean

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.view.ViewScoped;
import javax.inject.Named;

/**
 *
 * @author DIRGTI
 */
@Named
@ViewScoped
public class LocationController implements Serializable {

    private String cityName;
    private String postCode;
    private String selectedCountry;
    private List<Country> countries;

    @PostConstruct
    public void setup() {
        countries = new ArrayList<>();
        countries.add(new Country(1L, "Brazil"));
        countries.add(new Country(2L, "Peru"));
    }

    public void saveCity() {
        System.out.println("Action triggered");
    }

    public String getCityName() {
        return cityName;
    }

    public void setCityName(String cityName) {
        this.cityName = cityName;
    }

    public String getPostCode() {
        return postCode;
    }

    public void setPostCode(String postCode) {
        this.postCode = postCode;
    }

    public String getSelectedCountry() {
        return selectedCountry;
    }

    public void setSelectedCountry(String selectedCountry) {
        this.selectedCountry = selectedCountry;
    }

    public List<Country> getCountries() {
        return countries;
    }

    public void setCountries(List<Country> countries) {
        this.countries = countries;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM