java Spring 文件上传未绑定到模型属性对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17773777/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring file upload not binding to model attribute object
提问by marc82ch
I want to build a simple file upload functionality using Spring MVC.
我想使用 Spring MVC 构建一个简单的文件上传功能。
I have the multipartResolver in place and working:
我有 multipartResolver 到位并工作:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10240000"/>
</bean>
When uploading a file this is logged:
上传文件时,会记录以下内容:
DEBUG:[org.springframework.web.multipart.commons.CommonsMultipartResolver]: Found multipart file [imageUpload] of size 29081 bytes with original filename [xyz.jpg], stored at [/home/myuser/workspace/myProject/target/tmp/upload_67f1107c_1b8f_402c_bebd_6cd8a6e4c830_00000032.tmp]
which tells me that it's basically working.
这告诉我它基本上可以工作。
This is part of my JSP:
这是我的 JSP 的一部分:
<form:form modelAttribute="placeForm" action="/platz/save" method="post" cssClass="placeForm" enctype="multipart/form-data">
...
<label for="imageUpload">Upload</label>
<form:input type="file" path="imageUpload" id="imageUpload" accept="image/*" />
...
</form:form>
This is my model attribute object's class:
这是我的模型属性对象的类:
public class PlaceEditForm
{
@Valid
private Place place = new Place();
private Map<Integer, PlaceFeature> features;
private MultipartFile imageUpload;
... getter/setter omitted...
}
And this is part of my Controller method:
这是我的控制器方法的一部分:
@RequestMapping(value="/save", method=RequestMethod.POST)
public String savePlace (@Valid @ModelAttribute("placeForm") PlaceEditForm form, BindingResult result)
{
logger.debug("saveNewPlace");
logger.debug("Upload: "+form.getImageUpload()); // null
...
return "redirect:/platz/"+place.getPlaceId();
}
What happens is, that the imageUpload attribute of the form object is not populated (null), whereas all other form properties are.
发生的情况是,未填充表单对象的 imageUpload 属性(空),而所有其他表单属性都是。
I found that it does work when I use this in the controller:
我发现当我在控制器中使用它时它确实有效:
@RequestMapping(value="/save", method=RequestMethod.POST)
public String savePlace (@Valid @ModelAttribute("placeForm") PlaceEditForm form, BindingResult result, @RequestParam("imageUpload") MultipartFile upload, BindingResult uploadResult)
{
logger.debug("saveNewPlace");
logger.debug("Upload: "+upload); // Works!!
...
return "redirect:/platz/"+place.getPlaceId();
}
So, having the MultipartFile as a @RequestParam
works, but binding it to the form's modelAttribute
object doesn't. I found hundreds of examples on the web that do about the same and I don't find the difference.
因此,将 MultipartFile 作为@RequestParam
作品,但将其绑定到表单的modelAttribute
对象则不然。我在网上找到了数百个示例,它们的作用大致相同,但我没有发现它们之间的区别。
I'm still learning Spring, so I might miss a very obvious point. I could just use the second version of the controller, but I don't understand it, and as I said, I'm learning.
我还在学习 Spring,所以我可能会错过一个非常明显的点。我可以只使用控制器的第二个版本,但我不明白,正如我所说,我正在学习。
Shouldn't all the <form:input path="abc">
properties inside the <form:form modelAttribute="xyz">...</form:form>
be bound to xyz.abc
? It works like this for all properties except the file upload.
不应该将<form:input path="abc">
里面的所有属性<form:form modelAttribute="xyz">...</form:form>
都绑定到xyz.abc
?它适用于除文件上传之外的所有属性。
Any insights? Thanks
任何见解?谢谢
采纳答案by marc82ch
I found the problem:
我发现了问题:
I had a method like this in the controller, but forgot to add the imageUpload
property.
Very stupid and easy once found..!
我在控制器中有一个这样的方法,但忘记添加imageUpload
属性。一旦发现非常愚蠢和容易..!
@InitBinder
public void initBinder(WebDataBinder binder)
{
binder.setAllowedFields("place.placeId", "place.name", "place.description", "place.directions", "place.coordinates*", "features*", "tmpFiles*", "removeFiles*");
}
This prevents the binder to bind any other properties to the modelAttribute than the ones specified. Very important security measure to prevent evildoers from feeding in dangerous information into your system, when you only validate what you expect to be on the front-end.
这可以防止绑定器将任何其他属性绑定到 modelAttribute,而不是指定的属性。非常重要的安全措施,可防止作恶者将危险信息输入您的系统,当您只验证您期望在前端出现的内容时。