spring portlet mvc: model attribute is null despite being modelAttribute?

元气小坏坏 提交于 2019-12-11 16:33:58

问题


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>

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?

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


回答1:


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:

with spring tags:

<form:hidden path="username" />

or just with a form tag

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


来源:https://stackoverflow.com/questions/4385395/spring-portlet-mvc-model-attribute-is-null-despite-being-modelattribute

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!