0

我正在尝试创建一个 HIT(对象)列表,其中每个旁边都有一个复选框,以便我可以选择它们并一次删除它们。我为表格中的每一行制作了一个带有复选框的表格:

 final HashSet<HIT> selectedValues = new HashSet<HIT>(); 

 final CheckGroup checkgroup = new CheckGroup("checkgroup");

 final Form form = new Form("form"){
        @Override
        public void onSubmit() {
            super.onSubmit(); 
        }
    };

    checkgroup.add(new CheckGroupSelector("checkboxSelectAll"));

    UserHitDataProvider userHitDataProvider = new UserHitDataProvider(selectedIsReal, keyId, secretId);

    final DataView<HIT> dataView = new DataView<HIT>("pageable", userHitDataProvider) {
        private static final long serialVersionUID = 1L;

        @Override
        protected void populateItem(final Item<HIT> item) {
            HIT hit = item.getModelObject();
            item.add(new CheckBox("checkbox", new SelectItemUsingCheckboxModel(hit,selectedValues))); 

            item.add(new Label("hitName", String.valueOf(hit.getTitle())));
            item.add(new Label("hitId", String.valueOf(hit.getHITId())));
        }
    };

//add checkgroup to form, form to page, etc.

我还添加了一个新类来处理选择/删除:

public class SelectItemUsingCheckboxModel extends AbstractCheckBoxModel {

private HIT hit; 
private Set selection; 

public SelectItemUsingCheckboxModel(HIT h, Set selection) { 
    this.hit = h; 
    this.selection = selection; 
 } 

@Override 
public boolean isSelected() { 
    return selection.contains(hit); 
} 

@Override 
public void select() { 
    selection.add(hit); 
} 

@Override 
public void unselect() { 
    selection.remove(hit); 
}

}

一切都很好,但是在尝试提交时出现错误:

Caused by: java.lang.IllegalStateException: Attempt to set model object on null model of component: form:checkgroup
at org.apache.wicket.Component.setDefaultModelObject(Component.java:3042)
at org.apache.wicket.markup.html.form.FormComponent.updateCollectionModel(FormComponent.java:1572)
at org.apache.wicket.markup.html.form.CheckGroup.updateModel(CheckGroup.java:160)
at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:228)
at org.apache.wicket.markup.html.form.Form$FormModelUpdateVisitor.component(Form.java:198)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:274)
at org.apache.wicket.util.visit.Visits.visitPostOrderHelper(Visits.java:262)
at org.apache.wicket.util.visit.Visits.visitPostOrder(Visits.java:245)
at org.apache.wicket.markup.html.form.FormComponent.visitComponentsPostOrder(FormComponent.java:422)
at org.apache.wicket.markup.html.form.Form.internalUpdateFormComponentModels(Form.java:1793)
at org.apache.wicket.markup.html.form.Form.updateFormComponentModels(Form.java:1757)
at org.apache.wicket.markup.html.form.Form.process(Form.java:913)
at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:770)
at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:703)
... 27 more

我认为它的一些 Ajax 代码破坏,因为我的 SelectAllCheckBox 按钮也失败了。任何想法为什么?这甚至是处理此类用例的最佳方法吗?

4

1 回答 1

1

Checkgroup没有Model,因此 Wicket 无法将 的当前状态复制Model到 null object。您应该使用带有附加参数的构造函数,该参数表示Model您要在其中存储值。

于 2012-12-21T13:27:09.357 回答