Spring 3表单绑定对象始终为null
我正在尝试将嵌套对象与 Spring 3 绑定,但遇到了问题。
JSP:
<portlet:actionURL var="formAction" />
<form:form id="add-objects-form" method="post" action="${formAction}">
<input name = "obj.a"...>
<input name = "obj.b"...>
<input type = "file" multiple="multiple" name="file"/>
</form>
表单对象:
class FormObject{
private final static Logger logger = ...
private MultipartFile file
private Obj obj
...getters and setters
}
控制器:
@RequestMapping(method = RequestMethod.POST)
public void uploadDocument(@ModelAttribute FormObject formObject, BindingResult results ) {
}
formObject
获取 obj.a
和 obj.b
,但file
始终为空。
I'm trying to bind a nested object with Spring 3, and I'm having issues.
JSP:
<portlet:actionURL var="formAction" />
<form:form id="add-objects-form" method="post" action="${formAction}">
<input name = "obj.a"...>
<input name = "obj.b"...>
<input type = "file" multiple="multiple" name="file"/>
</form>
Form Object:
class FormObject{
private final static Logger logger = ...
private MultipartFile file
private Obj obj
...getters and setters
}
Controller:
@RequestMapping(method = RequestMethod.POST)
public void uploadDocument(@ModelAttribute FormObject formObject, BindingResult results ) {
}
formObject
gets obj.a
and obj.b
, but file
is always null.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
中添加modelAttribute="formObject"
还要确保您没有从类中排除调试信息。如果您已经或不确定,请指定
@ModelAttribute("formObject")
对于处理文件(多部分数据),您需要指定表单的 enctype:
更新:因为您使用的是 js-文件上传库,具体操作如下:
Add
modelAttribute="formObject"
in<form:form>
Also make sure you haven't excluded debug information from classes. If you have, or you are uncertain, specify
@ModelAttribute("formObject")
For handling files (multipart data) you need to specify the enctype for the form:
Update: since you are using a js-library for fileupload, here's what to do: