0

我在同一页面上有几个这样的表格:

<h:form>
    <h:selectOneMenu value="#{collectionBean.selectedCollection}">
        <f:selectItems value="#{collectionBean.collectionItems}" />
        <a4j:support event="onchange" />
    </h:selectOneMenu>
    <a4j:commandButton value="Add" action="#{collectionBean.addToCollection(_resource)}" >
    </a4j:commandButton>
</h:form>

这是我的豆:

@Name("collectionBean")
@Scope(ScopeType.SESSION)
public class CollectionBean {
    private String selectedCollection;

    public String getSelectedCollection() {
        return selectedCollection;
    }

    public void setSelectedCollection(String collectionName) {
        selectedCollection = collectionName;
    }

    public List<SelectItem> getCollectionItems() {
        ...
    }

    public void addToCollection(Resource res) {
        ...
    }
}

表单与资源相关联_resource,其目标是让用户将资源添加到他选择的集合中。

问题是只有页面上的最后一个表单有效:当更改其他表单中的选择时,该setSelectedCollection方法永远不会被调用。

你知道什么可能是错的吗?

4

1 回答 1

0

正如这里和评论中所说,将多个组件绑定到同一个 bean 属性是没有意义的。所以我在支持 bean 中使用了一个 Map,以资源 id 作为键。

<h:selectOneMenu value="#{collectionBean.selections[_resource.id]}">
    <f:selectItems value="#{collectionBean.collectionItems}" />
    <a4j:support event="onchange" />
</h:selectOneMenu>

尽管如此,它并没有解决主要问题:只有页面上的最后一个表单有效。对于所有其他形式,该方法getSelections从未被调用。

然后,我没有使用多个表单(每个选择菜单一个表单),而是使用了一个完整的表单。我不知道为什么,但它的工作...

于 2013-03-13T14:01:12.453 回答