We create a profile page with a form that has an optional profile. We are using Spring 3.2
Here is the form: -
<form:form id="editMember" modelAttribute="memberAjaxEditModel" method="POST" class="form-horizontal" enctype="multipart/form-data" > ... <form:input path="fileData" type="file"/> ... </form>
Here is the controller method: -
@RequestMapping(value = "/{id}", method = RequestMethod.POST) public String onEditPost(@PathVariable long id, @Valid @ModelAttribute(MemberAjaxEditModel.KEY) MemberAjaxEditModel model, BindingResult result) throws ServiceRecoverableException { .... }
Here is a model
public class MemberAjaxEditModel { ... private CommonsMultipartFile fileData; ... }
It works fine if the file is presented on the form, but there are errors in the BindingResult variable if the form is submitted without the file.
Here is the error: -
Field error in object 'memberAjaxEditModel' on field 'fileData': rejected value []; codes [typeMismatch.memberAjaxEditModel.fileData,typeMismatch.fileData,typeMismatch.org.springframework.web.multipart.commons.CommonsMultipartFile,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [memberAjaxEditModel.fileData,fileData]; arguments []; default message [fileData]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.springframework.web.multipart.commons.CommonsMultipartFile' for property 'fileData'; nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.web.multipart.commons.CommonsMultipartFile] for property 'fileData': no matching editors or conversion strategy found]
source share