I'm a novice user in Spring MVC. I tried designing a form uploading multiple files and redirected the action of the form submission to the controller url and like wise defined the command name of the form as the model attribute needed to be bind with. So i'm assuming the form parameters will be mapped as defined 'path' in the tags with the model attribute's parameters.
I'm posting my code below:
SampleForm.jsp
<form:form id="exampleFullForm" commandName="attribute1" enctype="multipart/form-data" method="post" autocomplete="off" novalidate="novalidate" class="fv-form fv-form-bootstrap" action="url1">
<form:input type="text" class="form-control" name="accnumber" path="account_number"/>
<form:input type="file" name="proof1" required="" file-model="proof1" path="files[0].file" id="files[0].file" class="filestyle" />
<form:input type="file" required="" name="proof2" file-model="proof2" path="files[1].file" id="files[1].file" class="filestyle" style="position:inherit;"/>
</form:form>
MyController.java
@RequestMapping(value = "url1", method = RequestMethod.POST,consumes="multipart/form-data")
public ModelAndView createNewMerchantAccount(@ModelAttribute("attribute1") ModelName modelParam, Map<String, Object> models) throws IOException{
//Do Something
return new ModelAndView("successpage");
}
Model.java
public Model() {
files.add(new FileBucket());
files.add(new FileBucket());
}
@Id
@Column(name = "id")
@GeneratedValue
private Long id;
@Column(name= "account_number")
private Integer account_number;
@Transient
private List<FileBucket> files = new ArrayList<FileBucket>();
//Getters and setters
public List<FileBucket> getFiles() {
return files;
}
public void setFiles(List<FileBucket> files) {
this.files = files;
}
After form submission, i get an error 400 bad Request. I'm attaching the snapshot of my post parameters as well. Files are getting attached properly as per my knowledge.
------WebKitFormBoundary4AkLoHMTlZVo90nY
Content-Disposition: form-data; name="files[0].file"; filename="Refund.png"
Content-Type: image/png
PNG
���
IHDR��4��2���Ä_����sRGB�®Îé���gAMA��±üa��� pHYs��Ä��Ä+��yÖIDATx^íÝ}+ë}Ø÷ßí?
`Ô2 Y#߯æú`Û+Vɬ
4ƶ$9ô 8
7îµY£
------WebKitFormBoundary4AkLoHMTlZVo90nY
Content-Disposition: form-data; name="files[1].file"; filename="Order_Refund.png"
Content-Type: image/png
èRÂê^Ýj»iQä@"ûWY¶±.sâTê^ì q�¹ËT¥n½=&ë»,rÚ@p¯dYìó<óÌðg^ÈåîYóý\Ì=$gæg^óÌÏËÎB�[ãõ7Þ7O
>mßU}ñòðáCû�����`{}þóɾ������;&������A@������ÀÖ ������`k0(°e��ÜÏ}îsöÕí{íµ×ì+��bzP
À! ��nh¾öß~`ñsÿ½Ú.M��P£������Ø*4������l
�������¶M����K;Ïa��XM����7kÞýÙÙïËÜ~��°.����ßÿÙÑHwÚÿ¬Ìõ¼µ¦¨IÓÿúæ��ÔG@���_ø?ËbñG2
24
------WebKitFormBoundary4AkLoHMTlZVo90nY
Content-Disposition: form-data; name="account_number"
FileBucket Model:
public class FileBucket {
MultipartFile file;
public MultipartFile getFile() {
return file;
}
public void setFile(MultipartFile file) {
this.file = file;
}
}
And my headers: Headers_attached_image
Help me to resolve the issue.
After struggling for hours, I figured out a way to rescue.
In my controller, I had forgot to add binding result and that's how i debugged the error:
@RequestMapping(value = "url1", method = RequestMethod.POST,consumes="multipart/form-data")
public ModelAndView createNewMerchantAccount(@ModelAttribute("attribute1") ModelName modelParam,BindingResult result, Map<String, Object> models) throws IOException{
//Do Something
if(result.hasErrors()) {
System.out.println("Result Error Occured"+result.getAllErrors());
}
return new ModelAndView("successpage");
}
And the error appeared to be in casting exception of one of the model's attribute.
And after casting the parameter to be of defined type, it worked like charm.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With