Ajax Call在jsf中的辅助bean中获取null值

时间:2013-09-21 09:50:51

标签: ajax jsf

我有两个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()始终在ajax调用中返回null,但我使用CommandButton调用相同的方法返回选定值,一切正常。

2 个答案:

答案 0 :(得分:1)

ajaxEvent班级

中实施bean

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
 }

here

了解更多有关JSF的信息

答案 1 :(得分:1)

<p:ajax>默认只处理父ClientBehavior组件,在您的特定情况下为<p:inputText>。换句话说,<p:ajax process>的实际值为@this。如果您确实想要处理整个表单,那么您应该在

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

process的{​​{1}}属性默认为<p:commandButton>,这就是它与之一起工作的原因。

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

@form