Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring upload form optional with optional file
    primarykey
    data
    text
    <p>We are creating a profile page with a form that optionally has a profile pic on it. We are using Spring 3.2 </p> <p>Here is the form: -</p> <pre><code>&lt;form:form id="editMember" modelAttribute="memberAjaxEditModel" method="POST" class="form-horizontal" enctype="multipart/form-data" &gt; ... &lt;form:input path="fileData" type="file"/&gt; ... &lt;/form&gt; </code></pre> <p>Here is the controller method: -</p> <pre><code>@RequestMapping(value = "/{id}", method = RequestMethod.POST) public String onEditPost(@PathVariable long id, @Valid @ModelAttribute(MemberAjaxEditModel.KEY) MemberAjaxEditModel model, BindingResult result) throws ServiceRecoverableException { .... } </code></pre> <p>Here is the Model</p> <pre><code>public class MemberAjaxEditModel { ... private CommonsMultipartFile fileData; ... } </code></pre> <p>It works fine if a file is submitted on the form, but there are errors in the BindingResult variable if the form is submitted without a file.</p> <p>Here is the error: -</p> <pre><code>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] </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload