0

我有两个 SelectoneMenu 和一个 inputText。在 inputText 中,我调用了一个 Ajax 事件(模糊)。但是这个 ajax 方法不会从 SelectoneMenu 中获取选定的值。但是我使用 CommandButton 调用了相同的方法,它将获得选定的值并且工作正常。

这是我的代码:

<h:form id=HarvestRateForm>
<table width="670px;">
                        <tr>
                            <td width="100px;"><p:outputLabel value="Crushing Season" for="crushingSeason"/></td>
                            <td width="80px;">
                                <p:selectOneMenu id="crushingSeason" style="width: 110px;" value="#{harvestRateBean.selectedSeason}">
                                    <f:selectItem itemValue="#{null}" itemLabel="Select"/>
                                    <f:selectItems value="#{harvestRateBean.seasons}"/>
                                </p:selectOneMenu><p:growl for="crushingSeason"/>
                            </td>
                            <td width="60px;" align="right"><p:outputLabel value="Plant" for="plant"/></td>
                            <td width="80px;"><p:selectOneMenu id="plant" style="width: 85px;" value="#{harvestRateBean.selectedPlant}">
                                    <f:selectItem itemValue="#{null}" itemLabel="Select"/>
                                    <f:selectItems value="#{harvestRateBean.plants}"/>
                                </p:selectOneMenu><p:growl for="plant"/></td>
                            <td width="60px;" align="right"><p:outputLabel value="Plot No" for="plotNo"/></td>
                            <td><p:inputText id="plotNo" value="#{harvestRateBean.sapPlotNo}" size="16"> 
                                    <p:ajax event="blur" listener="#{harvestRateBean.loadPlotDetails}"
                                            update="HarvestRateForm:plotNo HarvestRateForm:ryotCode"/>
                                </p:inputText>
                                <p:growl for="plotNo"/>
                            </td>
</tr>
<tr>
                            <td><p:outputLabel value="Ryot No" for="ryotCode"/></td>
                            <td><p:inputText value="#{harvestRateBean.ryotNo}" size="15" id="ryotCode" readonly="true" style="background: transparent;background-color: #cccccc"/></td>

我的豆类:

public class HarvestRateBean implements Serializable {

private Map<String, String> seasons;
private Map<String, String> plants;
private String selectedPlant;
private String selectedSeason;
private String sapPlotNo;
private String ryotNo;

public HarvestRateBean() {
    seasons = new HashMap<String, String>();
    plants = new HashMap<String, String>();
}
public Map<String, String> getSeasons() {
    List<Season> season_list = loadSeason();
    for (Iterator<Season> it = season_list.iterator(); it.hasNext();) {

        Season season1 = it.next();

        seasons.put(season1.getSeason(), season1.getSeason());
    }
    return seasons;
}

public void setSeasons(Map<String, String> seasons) {
    this.seasons = seasons;
}
public List<Season> loadSeason() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    List<Season> seasonlist = null;
    try {
        seasonlist = session.createCriteria(Season.class).list();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        session.close();
        HibernateUtil.getSessionFactory().close();
    }
    return seasonlist;
}

public Map<String, String> getPlants() {
    List<Plant> plant_list = loadPlants();
    for (Iterator<Plant> it = plant_list.iterator(); it.hasNext();) {

        Plant plant = it.next();

        plants.put(plant.getId().getPlant(), plant.getId().getPlant());
    }
    return plants;
}

public void setPlants(Map<String, String> plants) {
    this.plants = plants;
}

public List<Plant> loadPlants() {
    Session session = HibernateUtil.getSessionFactory().openSession();
    List<Plant> plantlist = null;
    try {
        plantlist = session.createCriteria(Plant.class).list();
    } catch (Exception ex) {
        ex.printStackTrace();
    } finally {
        session.close();
    }
    return plantlist;
}
public String getSelectedPlant() {
    return selectedPlant;
}

public void setSelectedPlant(String selectedPlant) {
    this.selectedPlant = selectedPlant;
}

public String getSelectedSeason() {
    return selectedSeason;
}

public void setSelectedSeason(String selectedSeason) {
    this.selectedSeason = selectedSeason;
}

public String getSapPlotNo() {
    return sapPlotNo;
}

public void setSapPlotNo(String sapPlotNo) {
    this.sapPlotNo = sapPlotNo;
}

public String getRyotNo() {
    return ryotNo;
}

public void setRyotNo(String ryotNo) {
    this.ryotNo = ryotNo;
}   
public void loadPlotDetails {
    Session session = HibernateUtil.getSessionFactory().openSession();
    List<HarvesterRate> rateList = null;
    FacesContext context = FacesContext.getCurrentInstance();
    try {
        if (getSelectedSeason() == null || getSelectedSeason().isEmpty()) {
            context.addMessage(null, new FacesMessage("Season is required", ""));
        } else if (getSelectedPlant() == null || getSelectedPlant().isEmpty()) {
            context.addMessage(null, new FacesMessage("Plant is required", ""));
        } else if (getSapPlotNo() == null || getSapPlotNo().isEmpty()) {
            context.addMessage(null, new FacesMessage("Plot No is required", ""));
        } else {
            rateList = session.createCriteria(HarvesterRate.class).add(Restrictions.eq("id.season", getSelectedSeason())).add(Restrictions.eq("id.plant", getSelectedPlant())).add(Restrictions.eq("id.plotNo", getSapPlotNo())).list();

            if (rateList.size() > 0) {
                for (Iterator<HarvesterRate> it = rateList.iterator(); it.hasNext();) {
                    HarvesterRate harvesterRate = it.next();
        setSapPlotNo(harvestRate.getPlotNo());
        setRyotNo(harvestRate.getVendorCode());
                }
            }
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}

getSelectedSeason()并且getSelectedPlant()总是在 Call 中返回 nullajax但我使用它调用了相同的方法CommandButton返回 Selected 值,一切正常。

4

2 回答 2

1

ajaxEventbean课堂上实施

Ajax 事件与普通方法不同,它调用侦听器作为输入,您可以Blur在文本字段上配置操作,如下所示

在 JSF 页面中

<p:inputText id="name" value="#{loginBean.name}">
        <f:ajax event="blur" listener="#{loginBean.ajaxEvent}" /> 
</p:inputText>  

确保以上内容必须在里面<h:form>

在 ManagedBean 类中

 public void ajaxEvent(AjaxBehaviorEvent e) 
 {
       //Do your stuffs
 }

了解更多 JSF 从这里开始

于 2013-09-21T11:08:15.150 回答
1

默认情况下,<p:ajax>将仅处理父ClientBehavior组件,在您的特定情况下是<p:inputText>. 换言之, 的实际<p:ajax process>值为@this。如果你真的想处理整个表单,那么你应该使用@formas

<p:inputText ...> 
    <p:ajax process="@form" ... />
</p:inputText>

默认为的process属性,这就是它与它一起工作的原因。<p:commandButton>@form

或者,如果您在表单中有更多输入组件并且打算只处理输入和这两个下拉列表,那么您可以指定(相对)客户端 ID 空间分隔:

<p:inputText ...> 
    <p:ajax process="crushingSeason plant @this" ... />
</p:inputText>
于 2013-09-22T19:33:55.593 回答