java - Spring file upload not binding to model attribute object -
i want build simple file upload functionality using spring mvc.
i have multipartresolver in place , working:
<bean id="multipartresolver" class="org.springframework.web.multipart.commons.commonsmultipartresolver"> <property name="maxuploadsize" value="10240000"/> </bean>
when uploading file logged:
debug:[org.springframework.web.multipart.commons.commonsmultipartresolver]: found multipart file [imageupload] of size 29081 bytes original filename [xyz.jpg], stored @ [/home/myuser/workspace/myproject/target/tmp/upload_67f1107c_1b8f_402c_bebd_6cd8a6e4c830_00000032.tmp]
which tells me it's working.
this part of 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 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 part of 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, imageupload attribute of form object not populated (null), whereas other form properties are.
i found work when use in 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 multipartfile @requestparam
works, binding form's modelattribute
object doesn't. found hundreds of examples on web same , don't find difference.
i'm still learning spring, might miss obvious point. use second version of controller, don't understand it, , said, i'm learning.
shouldn't <form:input path="abc">
properties inside <form:form modelattribute="xyz">...</form:form>
bound xyz.abc
? works properties except file upload.
any insights? thanks
i found problem:
i had method in controller, forgot add imageupload
property. stupid , easy once found..!
@initbinder public void initbinder(webdatabinder binder) { binder.setallowedfields("place.placeid", "place.name", "place.description", "place.directions", "place.coordinates*", "features*", "tmpfiles*", "removefiles*"); }
this prevents binder bind other properties modelattribute ones specified. important security measure prevent evildoers feeding in dangerous information system, when validate expect on front-end.
Comments
Post a Comment