2015-10-06 63 views
0

我刚刚在使用spring mvc更新窗体时出现了一个小问题。使用Spring MVC更新模型属性问题

以下是在提交POST方法的形式,游戏价值越来越更新后,我的代码片断

<form:form name="personInfo" method="post" modelAttribute="person"> 
<td class="empty"></td> 
<td class="editablefields">Football:</td> 
<td class="value"> 
<c:forEach items="${person.gameList}" var="game" varStatus = "count"> 
<c:if test="${game.code == 'FB'}"> 
<form:textarea path="gameList[${count.index }].value" id="" /> 
</c:if> 
</c:forEach> 
</td> 
</form:form> 

,但game.code即将为空。

请解释为什么会发生这种情况?

+0

你不会在表单中引用'game.code'作为表单输入。如果你想检索后处理程序中的值,添加'gameList [$ {count.index}]。code'作为一个隐藏的输入字段 –

+0

这是没有必要的。我做了类似的事情之后,它得到了正确的更新。在这种情况下,我错过了一些不能找到的东西 –

回答

1

您的标记需要提供person和其code属性之间的映射。否则,code中的值不会包含在处理程序方法的HTTP请求中。尝试改变你的循环:

<c:forEach items="${person.gameList}" var="game" varStatus = "count"> 
    <c:if test="${game.code == 'FB'}"> 
    <form:textarea path="gameList[${count.index }].value" id="" /> 
    <form:hidden path="gameList[${count.index }].code" value="${game.code}"/> 
    </c:if> 
</c:forEach> 
+1

'path =“gameList [$ {count.index}]。code”'可能更好,因为'code'是'game'的一个字段,而不是'人' –

+0

@NicolasLabrot你是对的 - 谢谢! –