Spring MVC 3 - 绑定模型元素中对象列表的属性

时间:2013-02-06 02:48:43

标签: data-binding spring-mvc

我有一个JSP生成的表单,需要向用户显示一个选项列表。其中一些是单实例选项(例如是否生成目录),并且工作正常。我现在需要添加一个从属选项列表,并允许用户打开或关闭每个选项。我可以在JSP页面中生成选项列表,并为每个选项提供唯一的ID。我希望在表单中用<form:checkbox/>元素表示一个。

选项类如下所示:

// 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;
    }
}

在控制器中的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中的逻辑无关紧要 - 它只会生成ArrayList ProductGroup个填充了我需要的数据的对象。

我遇到的问题是,我似乎无法说服Spring绑定到ProductGroup对象中各个PdfCatalogOptions对象中的字段。

JSP看起来像这样:

<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>&nbsp;</td>
        </tr>
        <c:forEach items="options.productGroups" var="productGroup">
            <tr>
                <td>&nbsp;</td>
                <td><form:checkbox path="productGroup.isSelected"/>blah</td>
            </tr>
        </c:forEach>
    </table>
</form:form>

在内部<c:forEach...>循环中,我无法找到使Spring绑定到单个ProductGroup对象的正确咒语,因此我最终可以获得ProductGroupisSelected个对象的列表{1}}根据用户要求设置属性。

如上所述,它抱怨说:

org.springframework.beans.NotReadablePropertyException: Invalid property 'productGroup' of bean class [<redacted>.PdfCatalogOptions]: Bean property 'productGroup' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

有人能指出我在正确的方向吗?


编辑1 :这正确列出了所有产品组:

<c:forEach items="${options.productGroups}" var="pg">
    <tr>
        <td>${pg.groupName}</td>
    </tr>
</c:forEach>

值存在并按我的预期显示。但是,我无法找到将值路径绑定到复选框的方法。如果我添加<form:checkbox path="pg.isSelected"/>,那么我告诉pg不是PdfCatalogOptions的属性。

如果我添加<form:checkbox path="${pg.isSelected}"/>,那么我有点可以预见 - true不是PdfCatalogOptions的属性。

0 个答案:

没有答案