Data binding Spring MVC 3-绑定模型元素中对象列表的属性
我有一个JSP生成的表单,需要向用户提供一个选项列表。其中一些是单实例选项,例如是否生成目录,这样做很好。我现在需要添加一个下级选项列表,并允许用户打开或关闭每个选项。我可以在JSP页面中生成选项列表,并为每个选项提供一个唯一的ID。我希望在表单中用一个元素表示一个选项 选项类如下所示:Data binding Spring MVC 3-绑定模型元素中对象列表的属性,data-binding,spring-mvc,Data Binding,Spring Mvc,我有一个JSP生成的表单,需要向用户提供一个选项列表。其中一些是单实例选项,例如是否生成目录,这样做很好。我现在需要添加一个下级选项列表,并允许用户打开或关闭每个选项。我可以在JSP页面中生成选项列表,并为每个选项提供一个唯一的ID。我希望在表单中用一个元素表示一个选项 选项类如下所示: // Option class public class PdfCatalogOptions { private boolean isTableOfContentsIncluded; priva
// Option class
public class PdfCatalogOptions
{
private boolean isTableOfContentsIncluded;
private List<ProductGroup> productGroups;
public boolean getTableOfContentsIncluded () {
return isTableOfContentsIncluded;
}
public PdfCatalogOptions setTableOfContentsIncluded ( final boolean isTableOfContentsIncluded ) {
this.isTableOfContentsIncluded = isTableOfContentsIncluded;
return this;
}
public List<ProductGroup> getProductGroups() {
return productGroups;
}
public PdfCatalogOptions setProductGroups( final List<ProductGroup> setProductGroups ) {
this.productGroups = productGroups;
return this;
}
}
public class ProductGroup
{
private String groupName;
private boolean isSelected;
public String getGroupName () {
return groupName;
}
public ProductGroup setGroupName ( final String groupName ) {
this.groupName = groupName;
return this;
}
public Boolean getIsSelected () {
return isSelected;
}
public ProductGroup setIsSelected ( final boolean selected ) {
isSelected = selected;
return this;
}
}
<form:form action="generateCatalog.do" commandName="options">
<table>
<tr>
<td colspan="3"><form:errors cssClass="error"/></td>
</tr>
<tr>
<td><spring:message code="catalog.include.toc"/></td>
<td><form:checkbox path="tableOfContentsIncluded"/></td>
</tr>
<tr>
<td> </td>
</tr>
<c:forEach items="options.productGroups" var="productGroup">
<tr>
<td> </td>
<td><form:checkbox path="productGroup.isSelected"/>blah</td>
</tr>
</c:forEach>
</table>
</form:form>
在控制器内的get处理程序中,我执行以下操作:
@RequestMapping( method = RequestMethod.GET )
public String get ( final Model model ) throws Exception {
model.addAttribute ( "options", new PdfCatalogOptions ().setProductGroups ( buildProductGroupList () ) );
return FormName.GENERATE_CATALOG;
}
buildProductGroupList中的逻辑无关紧要——它只生成一个ProductGroup对象的ArrayList,其中填充了我需要的数据
我遇到的问题是,我似乎无法说服Spring绑定到PdfCatalogOptions对象中各个ProductGroup对象内的字段
JSP如下所示:
// Option class
public class PdfCatalogOptions
{
private boolean isTableOfContentsIncluded;
private List<ProductGroup> productGroups;
public boolean getTableOfContentsIncluded () {
return isTableOfContentsIncluded;
}
public PdfCatalogOptions setTableOfContentsIncluded ( final boolean isTableOfContentsIncluded ) {
this.isTableOfContentsIncluded = isTableOfContentsIncluded;
return this;
}
public List<ProductGroup> getProductGroups() {
return productGroups;
}
public PdfCatalogOptions setProductGroups( final List<ProductGroup> setProductGroups ) {
this.productGroups = productGroups;
return this;
}
}
public class ProductGroup
{
private String groupName;
private boolean isSelected;
public String getGroupName () {
return groupName;
}
public ProductGroup setGroupName ( final String groupName ) {
this.groupName = groupName;
return this;
}
public Boolean getIsSelected () {
return isSelected;
}
public ProductGroup setIsSelected ( final boolean selected ) {
isSelected = selected;
return this;
}
}
<form:form action="generateCatalog.do" commandName="options">
<table>
<tr>
<td colspan="3"><form:errors cssClass="error"/></td>
</tr>
<tr>
<td><spring:message code="catalog.include.toc"/></td>
<td><form:checkbox path="tableOfContentsIncluded"/></td>
</tr>
<tr>
<td> </td>
</tr>
<c:forEach items="options.productGroups" var="productGroup">
<tr>
<td> </td>
<td><form:checkbox path="productGroup.isSelected"/>blah</td>
</tr>
</c:forEach>
</table>
</form:form>
有人能给我指一下正确的方向吗
编辑1:正确列出所有产品组:
<c:forEach items="${options.productGroups}" var="pg">
<tr>
<td>${pg.groupName}</td>
</tr>
</c:forEach>
这些值就在那里,并按照我的预期显示。但是,我不能做的是找到一种将值路径绑定到复选框的方法。如果我加上,我被告知pg不是PdfCatalogOptions的属性
如果我添加,那么我会被告知(某种程度上是可以预测的)true不是PdfCatalogOptions的属性。错误是意料之中的。PdfCatalogOptions中没有productGroup字段。它是ProductGroups productGroup是c:forEach循环变量。请检查生成的html。如果输入的类型复选框的名称包含productGroup而不是productGroups,则显然会发生此错误。@Andrew:您找到此问题的解决方案了吗??我也面临同样的问题。