发布绑定AutoPopulating List到一个表单Spring MVC [英] Issue Binding AutoPopulating List to a form Spring MVC

查看:248
本文介绍了发布绑定AutoPopulating List到一个表单Spring MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题,在表单中绑定AutoPupulating List来更新数据。虽然我可以使用自动填充列表保存数据。



以下是表单支持模式。

  public class AddUpdateShot {

private Integer shootId;
private char shotSelect;
private String shotNotes;
private Integer numOfItems;
private AutoPopulateList 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 { 

私人整数ID;
私人整数itemNum;
private String itemName;
private String colorCode;
private String colorName;

------ get和set方法

}} p>

当我第一次保存数据时,取决于用户需要多少个ItemColors,使用jquery,我动态地添加了输入字段,如下面的代码所示。

 < form:form id =createShootFormmethod =POST
commandName =createShoot>
< tr>
< td align =left>< label for =shootName> *拍摄名称:< / label>< / td>
< td>< form:input id =shootNameclass =requiredpath =shootName/>< / td>
< / tr>
-------形式支持obj中的其他输入字段----

< 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 =submittype =submitvalue =Next/>< / td>
< / tr>
< / table>
< / form:form>

上面的代码在我最初保存数据时工作得非常好。但是现在,当用户想要更新早先保存的数据时,我无法将Autopopulating列表绑定到JSP。

 < form:form id =updateShotFormmethod =POST
命令名= shotToUpdate >
----表单支持对象的其他输入字段---
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 =submittype =submit
value =Next/>
< / td>
< / table>
< / form:form>

当我打开编辑JSP时,出现以下运行时异常

  2011年9月7日上午10点38分00秒org.apache.catalina.core.StandardWrapperValve调用
SEVERE:Servlet.service()for servlet [jalapeno]在路径[/ OnLocation]中抛出异常[在256行处理JSP页面/WEB-INF/views/app/updateShot.jsp时发生异常

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:]有根本原因
org.springframework.beans.NotReadablePropertyException:bean类的无效属性'shotToUpdate'[com.jcrew.jalapeno.app.model.AddUpdateShot]:Bean属性'shotToUpdate'不可读或具有无效的getter方法:getter的返回类型是否与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)
在org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
在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)

我不确定为什么我无法将对象以这种方式绑定到表单,因为我的表单支持对象确实有一个Autopopulating List,它是我在加载此表单之前在控制器中初始化的 p>

  AutoPopulatingList itemNumColors = new AutoPopulatingList(ItemNumColor.class); (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

解决方案

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


I have an issue binding the AutoPupulating List in a form to update the data. I was able to save the data using Autopopulating list though.

Here is the form backing model.

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

--------

}

Where itemNumClors is a simple model

public class ItemNumColor {

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

------get and set methods    

}

When I first saved the data, depending on how many ItemColors the user wanted,using jquery I added the input fields dynamically as shown in the following code.

<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>

The above code worked perfectly fine when I initially saved the data. But now when the user want to update the earlier saved data, I am unable to bind the Autopopulating list to the JSP. Here is how am doing it.

<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>

When I open the edit JSP, I get the following run time exception

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)

I am not sure why I am not able to bind the object this way to the form since my form backing object does have an Autopopulating List which I initialised in the controller before loading this form

    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());

Any help is greatly appreciated.

Thanks, Shravanthi

解决方案

Remove the 'shotToUpdate.' keyword from the PATH attribute. You have already specified the command object name so the PATH attributes should be relative to the command object.

这篇关于发布绑定AutoPopulating List到一个表单Spring MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆