我們正在創建一個配置文件頁面,其中可能有一個配置文件圖片。我們使用Spring 3.2彈簧上傳表格可選帶有可選文件
這裏是形式: -
<form:form id="editMember" modelAttribute="memberAjaxEditModel"
method="POST" class="form-horizontal" enctype="multipart/form-data" >
...
<form:input path="fileData" type="file"/>
...
</form>
這裏是控制器方法: -
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String onEditPost(@PathVariable long id, @Valid @ModelAttribute(MemberAjaxEditModel.KEY) MemberAjaxEditModel model, BindingResult result) throws ServiceRecoverableException {
....
}
這裏是示範
public class MemberAjaxEditModel {
...
private CommonsMultipartFile fileData;
...
}
它工作正常如果在表單上提交了一個文件,但是如果表單沒有提交文件,那麼BindingResult變量就會出現錯誤。
以下是錯誤: -
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]
謝謝@Ash,我面臨着同樣的問題..它真的幫助我..只是,我用arr.splice(fileDataIndex,1);而不是arr.remove(fileDataIndex); – Saurabh 2013-06-27 11:12:47
它工作形式我也是,但當我在選項beforeSubmit中使用你的方法,網頁重新加載響應的內容(我失去了AJAX行爲)... – Labe 2015-05-18 14:59:07
我真的不知道爲什麼,但通過使用Saurabh修改,我的問題沒有了。謝謝 – Labe 2015-05-18 15:07:48