0
votes

In book "Spring in Action" i read , the default content type of a post submission is application/x-www-form-urlencoded and takes the form of name-value pairs separated by ampersands. (I believe these all goes as the body payload of the HTTP POST request.)

I further read, with enctype set to multipart/form-data, each field will be submitted as a distinct part of the POST request and not as just another name-value pair.

Q1> I don't get this line. I am from a REST background and will want to understand what in content of the HTTP POST request has changed ?

The server side code

@RequestMapping(method=RequestMethod.POST)
public String addSpitterFromForm(@Valid Spitter spitter,
    BindingResult bindingResult,
    @RequestParam(value="image", required=false)
Accept file upload
      MultipartFile image) {
if(bindingResult.hasErrors()) {
  return "spitters/edit";
}
spitterService.saveSpitter(spitter);
try {
  if(!image.isEmpty()) {
    validateImage(image);
Validate image
      saveImage(spitter.getId() + ".jpg", image); //
    }
  } catch (ImageUploadException e) {
    bindingResult.reject(e.getMessage());
    return "spitters/edit";
}
  return "redirect:/spitters/" + spitter.getUsername();
}

The client side code

    <sf:form method="POST"
                     modelAttribute="spitter"
                     enctype="multipart/form-data">
//other stuff
     <tr>
         <th><sf:label path="fullName">Full name:</sf:label></th>
         <td><sf:input path="fullName" size="15" /><br/>
             <sf:errors path="fullName" cssClass="error" />
         </td>
     </tr><tr>
      <th><label for="image">Profile image:</label></th>
      <td><input name="image" type="file"/>
    </tr>
//other stuff
    </sf:form>

From the code I am tempted to think that only the input type="file" is sent in a new way. Rest all are sent as key-value pairs. I think the book is also saying the same "When the form is submitted, it’ll be posted as a multipart form where one of the parts contains the image file’s binary data. "

Q2> If what i am thinking is correct, how does client know which input types to send as key-value pairs and whom to send individually?

1

1 Answers

1
votes

First of all, enctype of multipart/form-data IS NOT a Spring-MVC thing, it is an attribute of <form> in general web development, which means this attribute can be present in your HTML form regardless the server side technology. You can read more about it here: HTML 5 Candidate Recommendation [Specification] 4 The elements of HTML 4.10 Forms 4.10.22 Form submission 4.10.22.7 Multipart form data, also you can read specifically how the data will be sent by reading RFC2388. If you review it, you will see that data sent in a POST request with multipart/form-data is not a key/value pair anymore, instead it contains multiple parts (yes, it is multi part) where each part looks like this (example belongs to RFC2388):

--AaB03x
content-disposition: form-data; name="field1"
content-type: text/plain;charset=windows-1250
content-transfer-encoding: quoted-printable
Joe owes =80100.
--AaB03x

Note that Joe owes =80100. means Joe owes €100.

You can find another example in HTML 4 Specification where it shows a more concrete example when uploading two or more files (my comments are posted after <--):

Content-Type: multipart/form-data; boundary=AaB03x <-- mark for the whole request

--AaB03x <-- content of a part
Content-Disposition: form-data; name="submit-name" <-- field name

Larry <-- content of the field
--AaB03x <-- content of a part
Content-Disposition: form-data; name="files"
Content-Type: multipart/mixed; boundary=BbC04y

--BbC04y <-- content of a part containing a file
Content-Disposition: file; filename="file1.txt"
Content-Type: text/plain

... contents of file1.txt ...
--BbC04y <-- content of a part containing a file
Content-Disposition: file; filename="file2.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of file2.gif...
--BbC04y-- <-- end of parts containing file
--AaB03x-- <-- end of whole request data