ListMultipleChoice中的模型(wicket 1.5.4)

时间:2012-02-21 10:04:40

标签: java wicket wicket-1.5

当点击提交按钮时,我有奇怪的堆栈跟踪(此代码已呈现但不处理表单处理)。示例类:

public class SamplePage extends WebPage {

    private List<String> list = Arrays.asList(new String[] { "item1", "item2", "item3" });
    private List<String> selectedItem = Arrays.asList(new String[] { "item1" });

    public SamplePage(final PageParameters parameters) {
        super(parameters);

        Form<?> form = new Form<Void>("form");
        form.add(new Button("submin") {
            @Override
            public void onSubmit() {
                System.out.println("Selected");
                for (String tag : selectedItem)
                    System.out.println(tag);
            }
        });
        ListMultipleChoice<String> selector = new ListMultipleChoice<>("itemSelector",
            new PropertyModel<List<String>>(this, "selectedItem"), list);
        add(form);
        form.add(selector);
    }
    public List<String> getSelectedItem() {
        return selectedItem;
    }
    public void setSelectedItem(List<String> selectedItem) {
        this.selectedItem = selectedItem;
    }
} 

Caused by: java.lang.UnsupportedOperationException
    at java.util.AbstractList.remove(AbstractList.java:161)
    at java.util.AbstractList$Itr.remove(AbstractList.java:374)
    at java.util.AbstractList.removeRange(AbstractList.java:571)
    at java.util.AbstractList.clear(AbstractList.java:234)
    at org.apache.wicket.markup.html.form.FormComponent.updateCollectionModel(FormComponent.java:1531)
    at org.apache.wicket.markup.html.form.ListMultipleChoice.updateModel(ListMultipleChoice.java:369)
    at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:221)
    at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:192)
    at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:273)
    at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:261)
    at org.apache.wicket.util.visit.Visits.visitPostOrder(Visits.java:244)
    at org.apache.wicket.markup.html.form.FormComponent.visitComponentsPostOrder(FormComponent.java:388)
    at org.apache.wicket.markup.html.form.Form.internalUpdateFormComponentModels(Form.java:1701)
    at org.apache.wicket.markup.html.form.Form.updateFormComponentModels(Form.java:1666)
    at org.apache.wicket.markup.html.form.Form.process(Form.java:827)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:762)
    at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:692)
... 31 more

我如何理解问题在定义模型中,但我不明白为什么(我尝试了很多方法而没有检查问题)。当我以相同的方式创建DropDownChoice时,所有都正常工作,但在这种情况下我使用

new PropertyModel<String>(this, "field")

相反
new PropertyModel<List<String>>(this, "selectedItem")

我认为这是非常愚蠢的错误并请求你的帮助。

2 个答案:

答案 0 :(得分:4)

Arrays.asList()返回一个固定大小的列表,你(即Wicket)无法从中删除元素。

改为使用另一个列表:

private List<String> selectedItem = new ArrayList<String>();

public SamplePage(final PageParameters parameters) {
    ....
    selectedItem.add("item1");

答案 1 :(得分:2)

没有实际尝试,但我猜你从Arrays.asList()获得的List实现不支持删除列表中的条目。

尝试一下:

private List<String> selectedItem = 
                  new LinkedList(Arrays.asList(new String[] { "item1")});

E.g。使用允许删除的实现。 Wicket需要修改列表中的条目以重新选择所选项目。

希望有所帮助。伯特