我试图在其中显示一个带有复选框列表的表单。有两个Java类来保存我需要的数据:
public class CategoryAndSelection {
private Category category;
private boolean selection;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
public boolean getSelection() {
return selection;
}
public void setSelection(boolean selection) {
this.selection = selection;
}
public CategoryAndSelection() {
}
public CategoryAndSelection(Category category, boolean selection) {
super();
this.category = category;
this.selection = selection;
}
}
和
public class GroupAndCategoriesAndSelections {
private Group group;
private List<CategoryAndSelection> categoriesAndSelections;
public Group getGroup() {
return group;
}
public void setGroup(Group group) {
this.group = group;
}
public List<CategoryAndSelection> getCategoriesAndSelections() {
return categoriesAndSelections;
}
public void setCategoriesAndSelections(List<CategoryAndSelection> categoriesAndSelections) {
this.categoriesAndSelections = categoriesAndSelections;
}
public GroupAndCategoriesAndSelections() {
}
public GroupAndCategoriesAndSelections(List<Category> categories) {
this.group = new Group();
this.categoriesAndSelections = new ArrayList<CategoryAndSelection>();
for (Category category : categories) {
CategoryAndSelection categoryAndSelection = new CategoryAndSelection(category, false);
this.categoriesAndSelections.add(categoryAndSelection);
}
}
}
在我的控制器中,我确保初始化数据并将其作为属性添加到模型中:
CategoryRepository cr = new CategoryRepository(pm);
List<Category> catList = cr.getAllForProject(project, "title asc");
GroupAndCategoriesAndSelections groupAndCategoriesAndSelections = new GroupAndCategoriesAndSelections(catList);
model.addAttribute("groupAndCategoriesAndSelections", groupAndCategoriesAndSelections);
JSP文件的相应位如下所示:
<form:form method="POST" modelAttribute="groupAndCategoriesAndSelections">
<%-- ... --%>
<c:forEach items="${groupAndCategoriesAndSelections.categoriesAndSelections}" varStatus="status" >
<form:checkbox path="categoriesAndSelections[${status.index}].selection" value="categoriesAndSelections[${status.index}].selection" />
<br />
</c:forEach>
</form:form>
这可行,但出于某种原因,这并不是:
<form:form method="POST" modelAttribute="groupAndCategoriesAndSelections">
<%-- ... --%>
<form:checkboxes items="${categoriesAndSelections}" path="selection" />
</form:form>
由于某种原因,它会产生IllegalArgumentException: 'items' must not be null
。这是为什么?关于这个标签,我不明白什么?