1

您好,我看了很多教程和帖子,但您看不到问题所在。我有一个对话框,必须将数据保存到数据库中,调用托管 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 的参数中。你能帮助我吗?

4

2 回答 2

3

使用命令按钮添加 process="@this"

<p:commandButton value="Aceptar" process="@this"
                             actionListener="#{locationController.saveCity}" 
                             oncomplete="createCity.hide()" 
                             icon="ui-icon-save">
于 2013-10-17T05:48:58.747 回答
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;
    }
}
于 2013-10-16T17:13:01.317 回答