在我的渲染图中,我在模型(model.addAttribute)
中放置了一个User对象。
在耦合的JSP中我做了:
<form:form action="x" method="post" modelAttribute="user">
Username: ${user.username}
Age: ${age}
This information is correct: <input type="checkbox" id="correctInformation"/>
<input type="submit" value="Submit"/>
</form:form>
然而,在方法mathing x中,当我使用@ModelAttribute
标记检索用户对象时,用户对象是一个新实例,而不是表单中使用的实例(用户名为空等)。
有谁知道为什么会这样?解决方案?
编辑:
我可以使用<input type:hidden path="username"/>
并且它可以工作,但那不是那么干净......有更好的解决方案吗?
答案 0 :(得分:1)
问题与其他问题相同。除非为字段添加表单标记,否则不会在表单中发送信息。所以通常的方法是添加一个隐藏字段来发送这样的信息:
带弹簧标签的:
<form:hidden path="username" />
或仅使用表单标记
<input type="hidden" name="username" value=" ${user.username}" />