命令按钮不要激活动作 - Primefaces和JSF

时间:2013-10-16 17:03:24

标签: java jsf primefaces managed-bean

你好我看了很多教程和帖子,但你看不出问题。我有一个对话框,必须将数据保存到数据库中调用托管bean中的方法,但是当我调试方法时不要触发。我的页面代码是:

<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>

我在托管bean中的方法是:

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"));
    }

}

我尝试将javax.faces.event.ActionEvent放在方法的参数和nthing中。 你能帮助我吗?

2 个答案:

答案 0 :(得分:3)

使用命令按钮

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

答案 1 :(得分:0)

尝试使用:

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

我在您发布的代码中找不到 mainForm 。如果视图中没有 mainForm ,您将收到异常。如果您要更新 formCreateCity ,请改用update="@form"

以下是我用来测试它的代码:

视图

<?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>

实体

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;
    }
}

转换器

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();
    }
}

受管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;
    }
}