如何在selectOneMenu中重新填充我的列表?

时间:2012-11-10 05:03:58

标签: ajax jsf jsf-2

如何重新填充我的列表(stationaryList)?我的代码有什么问题?如果我选择value = CDL或MEL或NEF,我的列表中没有任何变化。感谢。

<h:form id="frmCreateNewStationary">
<ui:param name="s" value="#{myController.selected}"/>
<h:panelGrid columns="2">
    <h:outputLabel value="Type" for="idType" />
    <h:selectOneMenu id="idType" value="#{s.stationaryid.type}">
        <f:selectItem itemLabel="Select ..." noSelectionOption="true" />
        <f:selectItem itemLabel="CDL" itemValue="CDL" />
        <f:selectItem itemLabel="MEL" itemValue="MEL" />
        <f:selectItem itemLabel="NEF" itemValue="NEF" />
        <f:ajax event="change" listener="#{myController.changeStationaryCodeList}" render="idCode" execute="@this" />
    </h:selectOneMenu>
    <h:outputLabel value="Code" for="idCode" />
    <h:selectOneMenu id="idCode" value="#{s.stationaryid.code}">
        <f:selectItem itemLabel="Select ..." noSelectionOption="true" />
        <f:selectItems value="#{myController.stationaryList}" />
    </h:selectOneMenu>
</h:panelGrid>
</h:form>

爪哇

private List<Stationary> stationaryList = null;
public List<Stationary> getStationaryList() {
    stationaryList = getCode();
    return stationaryList;
}

public void setStationaryList(List<Stationary> stationaryList) {
    this.stationaryList = stationaryList;
}

...

public List<Stationary> getCode() {
    //"Stationary.ccc", query = "SELECT s.code FROM Stationary s"),
    EntityManager emf = facade.getEntityManager();
    Query query = emf.createNamedQuery("Stationary.ccc");
    return query.getResultList();        
}

...

public List<Stationary> getStationaryCodeItems(String type) {
    //"Stationary.code", query = "SELECT s.code FROM Stationary s WHERE s.type = :type"),
    EntityManager emf = facade.getEntityManager();
    Query query = emf.createNamedQuery("Stationary.code");
    query.setParameter("type", type);
    return query.getResultList();
}

AjaxBehaviorEvent看起来像这样:

public void changeStationaryCodeList(AjaxBehaviorEvent ev) {
    stationaryList.clear();
    String type = (String) ((UIInput) ev.getComponent()).getValue();
    System.out.println("Test__changeStationaryCodeList -- state == " + type);
    stationaryList = getStationaryCodeItems(type);
    //setStationaryList(stationaryList);
}

1 个答案:

答案 0 :(得分:0)

您的<h:selectOneMenu id="idType">没有设置value属性。将它绑定到您的支持bean中的值,就像您已使用<h:selectOneMenu id="idCode" value="#{s.stationaryid.code}">一样,然后可以在ajax侦听器中将该属性用作提交的值。我也有一种感觉,你可能没有在这里使用正确的范围。使用@ViewScoped注释对您的支持bean进行注释,以便在您的ajax中获得无障碍结果

与此无关,为什么你有一个名为“s”的支持bean?这只会让你的代码难以阅读。