0

我正在尝试找到一种从多个 p:selectManyCheckbox 填充地图的方法。但是,当我查看地图中的选定数据时,我得到了一个我无法访问的属性对象数组的数组。

<p:dataList id="serviceCatalogueCriteria" var="category" value="#{serviceCatalogueController.categories}">
<ui:fragment
    rendered="#{categoryService.isMultipleSelect(category)}">
    <p:selectManyCheckbox
            value="#{serviceCatalogueController.categoryToAttributes[category]}"
            layout="pageDirection" columns="1"
            converter="#{attributeConverter}">
            <f:selectItems value="#{category.attributes.toArray()}"
                var="attribute" itemLabel="#{attribute.name}"
                itemValue="#{attribute}" />
    </p:selectManyCheckbox>
</ui:fragment>
</p:dataList>

在后面的豆子里,我有

private Map<Category, List<Attribute>> categoryToAttributes = new HashMap<Category,List<Attribute>>();

for (Category cat : categoryToAttributes.keySet()) {
        for (Attribute attr : categoryToAttributes.get(cat)) {
            finalList.add(attributeDAO.fetchAttributeWithCategoryAndName(cat.getInternalName(), attr));
        }
    }

当我经历这个时,我得到了一个很好的

[Ljava.lang.Object; cannot be cast to java.util.List

在 categoryToAttributes.get(cat) 处爆炸。无法获得我也尝试使用转换器的对象,但我得到了几乎相同的东西。

转换器看起来像

@Override
public Object getAsObject(FacesContext context, UIComponent component,
        String value) {
    if (value == null || value.length() == 0)
        return null;
    try {
        Long id = Long.decode(value);
        return attributeDAO.fetchAttributesWithIds(Collections.singletonList(id)).get(0);
    } catch (NumberFormatException e) {
        return null;
    }
}

@Override
public String getAsString(FacesContext context, UIComponent component,
        Object value) {
    if (value == null || !(value instanceof Attribute))
        return null;
    return "" + ((Attribute)value).getId();
}

如果我卸下转换器,我会得到这个

java.lang.ClassCastException: [Ljava.lang.String; cannot be cast to java.util.List
4

1 回答 1

1

对不起,我的同行英语,我希望能帮助你。

在 *.xhtml

<p:dataList id="serviceCatalogueCriteria" var="category" 
value="#{serviceCatalogueController.categories}">

<ui:fragment
rendered="#{categoryService.isMultipleSelect(category)}">
<p:selectManyCheckbox
        value="#{serviceCatalogueController.attributesByCategory}"
        layout="pageDirection" columns="1"
        converter="#{attributeConverter}">
        <f:selectItems value="#{category.attributes.toArray()}"
            var="attribute" itemLabel="#{attribute.name}"
            itemValue="#{attribute}" />
<p:ajax listener="#{alta.almacenarModulos(category)}"/>
</p:selectManyCheckbox>
</ui:fragment>
</p:dataList>

在你的 Bean 上:

private Map<Category, List<Attribute>> attributesMap=new HashMap<>();
private List<Attribute> attributesByCategory;
public void almacenarModulos(Categorycategory)
   {
     attributesMap.put(category,attributesByCategory);
   }

我通过一个学者项目做了一些这样的事情。

于 2017-01-10T22:01:46.910 回答