我正在尝试找到一种从多个 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