Reputation: 73
Does my syntax is wrong to manage an array of files to upload?
In my java bean:
private List<Part> files = new ArrayList<Part>();
public void setFiles(List<Part> files) {
this.files = files;
}
public List<Part> getFiles() {
return files;
}
In my xhtml:
<div class="form-group">
<h:inputFile id="file1" value="#{theBean.files[0]}">
</h:inputFile>
<p:message for="file1"/>
<ui:fragment rendered="#{fn:length(theBean.files) > 1}">
<h:inputFile id="file2" value="#{theBean.files[1]}">
</h:inputFile>
</ui:fragment>
<ui:fragment rendered="#{fn:length(theBean.files) > 2}">
<h:inputFile id="file3" value="#{theBean.files[2]}">
</h:inputFile>
</ui:fragment>
</div>
I have an array of files to upload defined in my java code and I want to use the index of the array in the xhtml. The java code is not called. What is the better solution and keeping the list.
Upvotes: 1
Views: 100
Reputation: 1109132
Your list is empty.
System.out.println(files.size()); // 0
As Part
is immutable, you'd better use a list of mutable beans instead.
public class UploadedFile {
private Part value;
// Getter+setter.
}
private List<UploadedFile> files = new ArrayList<>();
@PostConstruct
public void init() {
files.add(new UploadedFile());
files.add(new UploadedFile());
System.out.println(files.size()); // 2
}
<h:inputFile value="#{bean.files[0].value}" />
Or alternatively use a plain vanilla array instead which you can preinitialize with a fixed size.
private Part[] files = new Part[2]; // 2 items.
Upvotes: 1