【问题标题】:How can I repopulate my list in selectOneMenu?如何在 selectOneMenu 中重新填充我的列表?
【发布时间】:2012-11-10 05:03:57
【问题描述】:

如何重新填充我的列表 (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>

Java

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

【问题讨论】:

    标签: ajax jsf jsf-2


    【解决方案1】:

    您的&lt;h:selectOneMenu id="idType"&gt; 没有value 属性集。将它绑定到支持 bean 中的一个值,就像您已经对 &lt;h:selectOneMenu id="idCode" value="#{s.stationaryid.code}"&gt; 所做的那样,然后您可以在您的 ajax 侦听器中使用该属性作为提交的值。我也有一种感觉,你可能在这里没有使用正确的范围。使用 @ViewScoped 注释来注释您的支持 bean,以便在您的 ajax-ing 中实现顺畅的结果

    与此无关,为什么你有一个名为“s”的支持bean?那只会让你的代码读起来很痛苦。

    【讨论】:

    • 嗨,我刚刚更新了我的代码,抱歉太简短了。我有 2 个表格:1 个编辑,1 个创建新表格。使用 Edit form 它运行良好,但对于 Create new form,它显示错误:javax.el.PropertyNotFoundException: /jsf/aircraftstationary/Create.xhtml @24,90 value="#{s.stationaryid.type}": Target Unreachable , 'null' 返回 null
    • 所以,如果我删除了:value="#{s.stationaryid.type}" 没有错误显示,但是我的List却无法改变,不知道为什么
    • @Peter,我不是说你应该复制粘贴相同的`value="#{s.stationaryid.type}"`到idType。我的意思是你应该在你的支持 bean 上创建一个String 属性(带有相应的 getter 和 setter)并将其绑定到下拉菜单,如value="#{yourBackingBean.theVariable}" 中。您是否也将您的支持 bean 更改为 @ViewScoped bean?您是否从某个地方复制了这段代码?
    • 非常感谢您的帮助,上次我使用@SessionScoped bean。对于这段代码,我自己做,我认为当对 selectOneMenu 列表进行硬编码时,Listerner 可以理解 CDL、MEL、NEF 值,因此我的列表可以重新填充。再次感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-21
    • 1970-01-01
    • 2013-09-16
    相关资源
    最近更新 更多