• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

File upload array in Struts

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am using a bean to dynamically get populate no of file uploads in a page from the bean.

The HTML is getting generated as

In the the Form Class


In the action class when i try to retrieve the data from
It is giving null. Can some one help me where I am going wrong.
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When using indexed properties you must provide an indexed getter and setter, which you have not. You must also initialize the array to the number of elements you expect. If you don't know how many elements, use a java.util.List instead of an array. Also, indexed="true" works only for an array or list of JavaBeans, not for a simple array. For a simple array, you have to build the the indexed property name yourself. Example:

new accessors:

new JSP entry:
 
Joe Koppole
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Merrill,
It did work. But was a little curious about the usage of List. Can you give me some pointer how to use it. The number of components is dynamic and is not restricted.

Thanks
Joe
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Tekkie Joe,

I'm glad it worked.

Before we go any further, though, one of my duties as forum moderator is to see that the guidelines are followed, and I noticed the name you are currently displaying does not meet the guidelines of the JavaRanch Naming Policy. In particular, the name you display must be a first and last name, and must not be obviously fictitious. Please change the Publicly Displayed Name in your user profile so that it meets the guidelines.

This may seem trivial to you, but it's an important part of the culture at JavaRanch, and we do appreciate your compliance with it.

Regarding your question about using a List, you would just change the underlying representation of customDocs variable from an array to a List. You would then modify the getter and setter accordingly. You would also have to give your accessors "lazy initialization" behavior. For more information and an example of this, see question 6 of this forum's FAQ.
 
Joe Koppole
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well Merrill, Thanks.. Well Just added a caption as usual. Didn't know that it will create problems any way changed it Thanks for your help
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

According to above discussion about multiple file upload in struts 1.x, I am also facing the same problem, i am trying to upload dynamically multiple files through action class, but i have referred above discussion and tried to incorporate in my code but when i login and click on my jsp page error is coming as "Cannot create iterator for this collection"

here is my code

Jsp:

<logic:iterate id="docName" name="Formclass" indexId="index" >
<td class="detailsPageValue">
<html:file property='<%="selectfile["+index+"]" %>' />
</td>
</logic:iterate>

Formclass.java

private FormFile[] selectfile = new FormFile[100];

public FormFile[] getSelectfile() {
return selectfile;
}

public void setSelectfile(FormFile[] selectfile) {
this.selectfile = selectfile;
}

and I am not getting how to read and write multiple files in action class when we submit the form..
Please Suggest me with this, thanks in advance.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep venkatesh wrote:i have referred above discussion and tried to incorporate in my code



I don't think you did because if you look at the method signatures Merrill suggests, you will see that yours do not match.
I question the utility of this approach. Why are you initializing the list of files on the JSP? Wouldn't that require you to know what files exist on the client?
 
Pradeep venkatesh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No , I do not need to know what files are on the client side, But i am suppose to allow users only image files to upload, so i thought of using that in my code, can you please give me any suggestions for how to make this thing work?I mean I want to upload multiple files in struts 1.x when i submit the form all files should save in folder in local drive.

Thanks in advance.
Pradeep.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pradeep venkatesh wrote:i am suppose to allow users only image files to upload, so i thought of using that in my code, .



I have no idea how that approach would work. The file tag has an "accept" parameter where one can specify a list of MIME types that are allowed. I assume you could also use server-side validation to check the submitted file names, but that would not be idea as the data would have to be transferred before the check can be done.
 
Pradeep venkatesh
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Joe Ess,

Thank you so much for the suggestions, I will check the file types using server side validations.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am very new to struts, and i am trying to upload multiple files, we have dynamic add button whenever i click on that button a new row will be added to upload file but with the same property, every time i want to change the property when i add new row , so i was going through the internet i got this solution with the below code, but i am not getting how to work with the below code , can anyone help me with this? any help will be appreciated.

Can any one please tell me how the below code works?

<logic:iterate id="docName" name="upLoadList" indexId="index" >
<tr>
<td class="attributeName">
<bean:message name="docName" locale="locale1" /></td>
<td class="value"> <html:file property='<%="customDoc["+index+"]" %>' /></td>
</tr>
</logic:iterate>


how this code works? and what is "upLoadList" ? from where the values are getting generate for this list?
is that every time i add one more row the property can be changed dynamically? with the index ?. please help.

Thank you.
 
Joe Ess
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Harshith kumar wrote:
how this code works? .



I indicated above that I have no idea how this code should work. To me it looks like the server would have to have a list of files the client is supposed to upload.
Where did you find this code?
 
reply
    Bookmark Topic Watch Topic
  • New Topic