【发布时间】:2013-03-08 07:16:55
【问题描述】:
下面是我的代码...
@RequestMapping(value = "/submitnewstory", method = RequestMethod.POST)
@ResponseBody
public String save(@ModelAttribute("storyInsertForm") StoryInsertForm uploadForm, HttpSession session, Model map) {
}
HTML 代码:
<form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">
</form:form>
发生了什么?
假设我的模型中有两个字段(在本例中为 StoryInsertForm),即 String firstname 和 String lastname。我必须在<form:form> 中声明相同的变量,如下所示
<form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">
<input type="text" name="firstname" id="firstname" />
<input type="text" name="lastname" id="lastname" />
</form:form>
发生的情况是,如果您给第一个输入一个值而忽略第二个输入,则数据甚至不会到达控制器。但是,如果您提供所有值,即为两种输入类型文本提供输入,它会找到控制器并且一切正常。
另一个观察表明,如果您不声明其中一个字段,即类似于以下内容:
<form:form id="contentForm" class="cssform2" action="submitnewstory" method="post" enctype="multipart/form-data" modelAttribute="storyInsertForm">
<input type="text" name="firstname" id="firstname" />
<!-- <input type="text" name="lastname" id="lastname" /> -->
</form:form>
数据仍会与上述字段中提供的数据一起发布(在本例中为名字)。
总结
如果您已关联 html 中的字段,则必须在输入类型中输入一些值,否则控制器将无法接收任何内容。
期待您的帮助。
【问题讨论】:
标签: html spring-mvc controller spring-roo