簡體   English   中英

命令按鈕不要激活動作 - Primefaces和JSF

[英]Command button dont fire action - Primefaces and JSF

你好,我看了很多教程和帖子,但你看不出問題。 我有一個對話框,必須將數據保存到數據庫中調用托管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中。 你能幫助我嗎?

使用命令按鈕添加process =“@ this”

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

試試這個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM