Spring Portlet MVC:尽管是modelAttribute,但是model属性仍为null? [英] spring portlet mvc: model attribute is null despite being modelAttribute?

查看:265
本文介绍了Spring Portlet MVC:尽管是modelAttribute,但是model属性仍为null?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的rendermapping中,我在模型中放置了一个User对象. 在耦合的JSP中,我这样做:

In my rendermapping I put a User object in my model(model.addAttribute). In the coupled JSP I do:

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

但是在方法x中,当我使用@ModelAttribute标记检索用户对象时,该用户对象是一个新实例,而不是表单中使用的实例(用户名为空等).

However in the method mathing x when I retrieve the user object using an @ModelAttribute tag the user object is a new instance instead of the one used in the form (the username is empty etc).

有人知道为什么会这样吗?如何解决?

Does anyone know why this happens and the solution?

我可以使用<input type:hidden path="username"/>,它可以工作,但是那不是很干净.是否有更好的解决方案?

edit: I can use <input type:hidden path="username"/> and it works but that isn't really that clean... Is there a better solution?

推荐答案

问题与另一个问题相同.除非您为字段添加表单标签,否则您不会在表单中发送信息.因此,通常的做法是添加一个隐藏字段以发送如下信息:

The problem is the same as the other question. You are not sending the information in the form unless you add a form tag for the fields. So the usual way to go is to add a hidden field to send this information like this:

带有弹簧标签:

<form:hidden path="username" />

或仅带有表单标签

<input type="hidden" name="username" value=" ${user.username}" />

这篇关于Spring Portlet MVC:尽管是modelAttribute,但是model属性仍为null?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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