简体   繁体   English

JSF 输入文件:要上传的文件数组(语法)

[英]JSF inputfile : array of files to upload (syntax)

Does my syntax is wrong to manage an array of files to upload?我的语法错误管理要上传的文件数组吗?

In my java bean:在我的 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:在我的 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.我有一个在我的 java 代码中定义的要上传的文件数组,我想在 xhtml 中使用该数组的索引。 The java code is not called.不调用 java 代码。 What is the better solution and keeping the list.什么是更好的解决方案并保留列表。

Your list is empty.您的列表是空的。

System.out.println(files.size()); // 0

As Part is immutable, you'd better use a list of mutable beans instead.由于Part是不可变的,因此您最好使用可变 bean 列表。

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.或者,也可以使用普通的 vanilla 数组,您可以使用固定大小对其进行预初始化。

private Part[] files = new Part[2]; // 2 items.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM