Tworzymy stronę profilu z formularzem, który opcjonalnie ma zdjęcie profilowe. Używamy Wiosna 3,2Wiosenny formularz wysyłania opcjonalny z opcjonalnym plikiem
Oto postać: -
<form:form id="editMember" modelAttribute="memberAjaxEditModel"
method="POST" class="form-horizontal" enctype="multipart/form-data" >
...
<form:input path="fileData" type="file"/>
...
</form>
Oto metoda kontroler: -
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String onEditPost(@PathVariable long id, @Valid @ModelAttribute(MemberAjaxEditModel.KEY) MemberAjaxEditModel model, BindingResult result) throws ServiceRecoverableException {
....
}
Oto model
public class MemberAjaxEditModel {
...
private CommonsMultipartFile fileData;
...
}
Współpracuje jeśli plik jest przesłany w formularzu, ale występują błędy w zmiennej BindingResult, jeśli formularz jest przesłany bez pliku.
Tutaj jest błąd: -
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]
Dzięki @ Ash, miałem do czynienia z tym samym problemem .. to naprawdę mi pomogło .. Po prostu użyłem arr.splice (fileDataIndex, 1); zamiast arr.remove (fileDataIndex); – Saurabh
To działa również na mnie, ale kiedy używam twojej metody w opcji beforeSubmit, strona jest przeładowywana treścią odpowiedzi (tracę zachowanie AJAX) ... – Labe
Naprawdę nie wiem dlaczego, ale używając modyfikacji Saurabh, mój problem zniknął. Dzięki – Labe