我试图将其扩展到selectManyMenu的可变长度列表.由于它们是selectMany,因此我希望能够绑定到Map< String,List< MyObject>>.我有一个示例,可以在其中将单个selectManyMenu绑定到List< MyObject>.一切正常,但是乳清我在ui:repeat中放入了动态数量的selectManyMenus,并尝试绑定到地图,结果结果很奇怪.这些值已正确地存储在映射中,如调试器所验证,并调用toString(),但是运行时认为映射的值是Object类型的,而不是List< MyObject>.并在尝试访问地图的键时引发ClassCastExceptions.
我猜想这与JSF如何确定绑定目标的运行时类型有关,并且由于我绑定到Map中的值,因此它不知道从地图.除了修补Mojarra以外,是否有其他解决方法?
总的来说,我怎么能有一个带有动态数量的selectManyMenus的页面?没有,当然要使用Primefaces的< p:solveThisProblemForMe>零件. (实际上,由于我无法控制的因素,Primefaces在这里不是一个选择.)
UISelectMany on a List
JSF:
??????
????????
??????
转换器:
@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);
}
}
后备豆:
@Data
@Slf4j
@Named
@ViewScoped
public class TestBean implements Serializable {
private static final long serialVersionUID = 1L;
@Inject
ItemStore itemStore;
List- availableItems;
List
itemCategories;
Map> 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- items = entry.getValue();
log.debug("Key: {}", key);
items.forEach(item -> {
log.debug(" Value: {}", item);
});
});
}
}
ItemStore仅包含HashMap和委托方法,以按其ID字段访问Item.
项目:
@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- items = entry.getValue();
我在这里想念什么?
本文由 投稿者 创作,文章地址:https://blog.isoyu.com/archives/shuzu-uirepeatzhongdeuiselectmanydaozhijava-lang-cla.html
采用知识共享署名4.0 国际许可协议进行许可。除注明转载/出处外,均为本站原创或翻译,转载前请务必署名。最后编辑时间为:11 月 12, 2019 at 05:23 下午