尝试在组件的null模型上设置模型对象:form:checkgroup

时间:2012-12-20 04:02:54

标签: wicket

我正在尝试创建一个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按钮也失败了。有什么想法吗?这甚至是处理这种用例的最佳方式吗?

1 个答案:

答案 0 :(得分:1)

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