将AutoPopulating List绑定到表单Spring MVC

时间:2011-09-07 14:49:37

标签: forms spring-mvc auto-populating

我在表单中绑定AutoPupulating List以更新数据时遇到问题。我可以使用Autopopulating列表保存数据。

这是表单支持模型。

public class AddUpdateShot {

private Integer shootId;
private char shotSelect;
private String shotNotes;   
private Integer numOfItems;
private AutoPopulatingList itemNumColors;
private Integer totalNumOfItems;
private String shotName;

----------

public void setItemNumColors(AutoPopulatingList  itemNumColors){
    this.itemNumColors = itemNumColors;
}

public AutoPopulatingList getItemNumColors(){
    return this.itemNumColors;
}

--------

}

其中itemNumClors是一个简单的模型

public class ItemNumColor {

private Integer id;
private Integer itemNum;
private String itemName;
private String colorCode;
private String colorName;

------get and set methods    

}

当我第一次保存数据时,根据用户想要的ItemColors数量,使用jquery我动态添加了输入字段,如下面的代码所示。

<form:form id="createShootForm" method="POST"
            commandName="createShoot">
<tr>
<td align="left"><label for="shootName">*Shoot Name:</label></td>
<td><form:input id="shootName" class="required" path="shootName" /></td>
</tr>
 ------- other input fields in form backing obj----

<c:forEach var="i" begin="${start}" end="${end-1}" step="1" varStatus="status">
<tr>
    <td align="left"><label for="itemNumber${i}">Item
            Number${i+1}:</label></td>
    <td><form:input id="itemNumber${i}"
            path="createShoot.itemNumColors[${i}].itemNum" /></td>
    <td><form:select id="color${i}"
            path="createShoot.itemNumColors[${i}].colorCode">
            <form:option value="" label="Color" />
        </form:select>
    </td>
</tr>
</c:forEach>
<tr id="submitRow">
<td></td>
<td></td>
<td align="right"><input name="submit" type="submit" value="Next" /></td>
</tr>
</table>
</form:form>

当我最初保存数据时,上面的代码完全正常。但是现在当用户想要更新先前保存的数据时,我无法将自动填充列表绑定到JSP。这是我在做的事情。

<form:form id="updateShotForm" method="POST"
            commandName="shotToUpdate">
----other input fields of form backing object---
<c:forEach var="i" begin="0" end="${totalNumOfItems-1}" step="1"
                    varStatus="status">
<tr><td align="left"><label for="itemNumber${i}">ItemNumber${i+1}:</label></td>  
<td><form:input id="itemNumber${i}"path="shotToUpdate.itemNumColors[${i}].itemNum"  /></td> 
</tr>
</c:forEach>
<tr id="submitRow">
<td></td>
<td></td>
<td align="right"><input name="submit" type="submit"
                        value="Next" />
</td>
</table>
</form:form>

当我打开编辑JSP时,我得到以下运行时异常

Sep 7, 2011 10:38:00 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jalapeno] in context with path [/OnLocation] threw exception [An exception occurred processing JSP page /WEB-INF/views/app/updateShot.jsp at line 256

253:   <tr>
254:    <td align="left"><label for="itemNumber${i}">Item
255:                Number${i+1}:</label></td>
256:    <td><form:input id="itemNumber${i}"
257:        path="shotToUpdate.itemNumColors[${i}].itemNum" /></td>
258:    <td><form:select id="color${i}"
259:        path="shotToUpdate.itemNumColors[${i}].colorCode">

Stacktrace:] with root cause
org.springframework.beans.NotReadablePropertyException: Invalid property 'shotToUpdate' of bean class [com.jcrew.jalapeno.app.model.AddUpdateShot]: Bean property 'shotToUpdate' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:707)
at org.springframework.beans.BeanWrapperImpl.getNestedBeanWrapper(BeanWrapperImpl.java:555)
at org.springframework.beans.BeanWrapperImpl.getBeanWrapperForPropertyPath(BeanWrapperImpl.java:532)
at org.springframework.beans.BeanWrapperImpl.getPropertyValue(BeanWrapperImpl.java:697)
at org.springframework.validation.AbstractPropertyBindingResult.getActualFieldValue(AbstractPropertyBindingResult.java:98)
at org.springframework.validation.AbstractBindingResult.getFieldValue(AbstractBindingResult.java:224)
at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:120)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:123)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:408)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)

我不知道为什么我无法以这种方式将对象绑定到表单,因为我的表单支持对象确实有一个自动填充列表,我在加载此表单之前在控制器中初始化了

    AutoPopulatingList itemNumColors = new AutoPopulatingList(ItemNumColor.class);

    for( OnLocShotItemNumber onLocItemNumColor : itemNumColorsList){
        ItemNumColor itemColor = new ItemNumColor();
        itemColor.setId(onLocItemNumColor.getId());
        itemColor.setColorCode(onLocItemNumColor.getItemColorCode());
        itemColor.setItemNum(onLocItemNumColor.getItemNumber());
        itemNumColors.add(itemColor);
    }

    shotToUpdate.setItemNumColors(itemNumColors);

model.put("shotToUpdate", shotToUpdate);
model.put("totalNumOfItems", itemNumColorsList.size());

非常感谢任何帮助。

谢谢, Shravanthi

1 个答案:

答案 0 :(得分:1)

删除'shotToUpdate'。 PATH属性中的关键字。您已经指定了命令对象名称,因此PATH属性应该相对于命令对象。