【发布时间】:2016-03-25 13:44:23
【问题描述】:
我使用HashMap 方法成功地将复选框列表绑定到Map<String, Boolean>。这很好,因为它允许您拥有动态数量的复选框。
我正在尝试将其扩展到selectManyMenu 的可变长度列表。由于它们是 selectMany,我希望能够绑定到 Map<String, List<MyObject>>。我有一个工作示例,我可以将单个 selectManyMenu 绑定到 List<MyObject> 并且一切正常,但是我将动态数量的 selectManyMenus 放入 ui:repeat 并尝试绑定到地图,我最终结果很奇怪。值正确存储在映射中,由调试器验证,并调用toString(),但运行时认为映射的值类型为Object 而不是List<MyObject>,并在我尝试访问映射的键时抛出 ClassCastExceptions .
我猜这与 JSF 如何确定绑定目标的运行时类型有关,并且由于我绑定到 Map 中的值,因此它不知道从中获取类型地图的值类型参数。除了可能修补 Mojarra 之外,还有其他解决方法吗?
一般来说,我怎样才能拥有一个带有动态数量的 selectManyMenus 的页面?当然没有使用 Primefaces 的 <p:solveThisProblemForMe> 组件。 (说真的,Primefaces 在这里不是一个选择,因为我无法控制。)
UISelectMany on a List<T> causes java.lang.ClassCastException: java.lang.String cannot be cast to T 的问题有一些我不知道的好信息,但我仍然对这个 SSCE 有疑问:
JSF:
<ui:define name="content">
<h:form>
<ui:repeat value="#{testBean.itemCategories}" var="category">
<h:selectManyMenu value="#{testBean.selectedItemMap[category]}">
<f:selectItems value="#{testBean.availableItems}" var="item" itemValue="#{item}" itemLabel="#{item.name}"></f:selectItems>
<f:converter binding="#{itemConverter}"></f:converter>
<f:validator validatorId="test.itemValidator"></f:validator>
</h:selectManyMenu>
</ui:repeat>
<h:commandButton value="Submit">
<f:ajax listener="#{testBean.submitSelections}" execute="@form"></f:ajax>
</h:commandButton>
</h:form>
</ui:define>
转换器:
@Named
public class ItemConverter implements Converter {
@Inject
ItemStore itemStore;
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return itemStore.getById(value);
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
return Optional.of(value)
.filter(v -> Item.class.isInstance(v))
.map(v -> ((Item) v).getId())
.orElse(null);
}
}
支持 Bean:
@Data
@Slf4j
@Named
@ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
ItemStore itemStore;
List<Item> availableItems;
List<String> itemCategories;
Map<String, List<Item>> selectedItemMap = new HashMap<>();
public void initialize() {
log.debug("Initialized TestBean");
availableItems = itemStore.getAllItems();
itemCategories = new ArrayList<>();
itemCategories.add("First Category");
itemCategories.add("Second Category");
itemCategories.add("Third Category");
}
public void submitSelections(AjaxBehaviorEvent event) {
log.debug("Submitted Selections");
selectedItemMap.entrySet().forEach(entry -> {
String key = entry.getKey();
List<Item> items = entry.getValue();
log.debug("Key: {}", key);
items.forEach(item -> {
log.debug(" Value: {}", item);
});
});
}
}
ItemStore 只包含一个 HashMap 和通过 ID 字段访问 Items 的委托方法。
项目:
@Data
@Builder
public class Item {
private String id;
private String name;
private String value;
}
ItemListValidator:
@FacesValidator("test.itemValidator")
public class ItemListValidator implements Validator {
@Override
public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException {
if (List.class.isInstance(value)) {
if (((List) value).size() < 1) {
throw new ValidatorException(new FacesMessage(FacesMessage.SEVERITY_FATAL, "You must select at least 1 Admin Area", "You must select at least 1 Admin Area"));
}
}
}
}
错误:
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to java.util.List
Stacktrace 被截断但出现在这一行:
List<Item> items = entry.getValue();
我在这里错过了什么?
【问题讨论】:
标签: arrays jsf classcastexception uirepeat selectmanymenu