When trying to upload multiple files with Struts2 using ArrayList
, how to identify each field?
For example, if I have two file fields, File1
and File2
and on the client side, I choose to upload only File2
, Struts 2 only creates one element in the list and I am not able to correctly map File1
as empty and File2
with the uploaded file.
Is there any identifier I can use here?
Name each field with different names and create corresponding accessors to action properties. Then each of them will handle the name by OGNL and set corresponding properties. Try with different approach create list or map for the files indexed by iterator tag.
in action
also accessors should be for each property
then you will know which of them uploaded by checking the file at the list index. You could also try with the
Map
.in action
what is better suits your needs
Use a List (or Map) of a custom object, containing your File, the fileName and the contentType (and eventually other fields). An JSP row corresponding element, to be clear.
I did like that (because I had many other fields too) and it works like a charm.
(this is not the only way, it is just one of the working ways that will become even more handy when you will need to handle additional fields too)
POJO
in Action
in JSP
File name and content-type will be automatically detected and populated by Struts2 (ensure you have FileUploadInterceptor in your stack).