0

请让我知道你的建议。我想构建具有特定设计的 fileUpload,并且在获取 uploadFile 时遇到问题。

我的设计:

在此处输入图像描述

数据模型类:

@Named
@SessionScoped
public class NJDataModel implements Serializable {
    private static final long serialVersionUID = 1L;

    private String sectionName;
    private UploadedFile file;

    public NJDataModel(String sectionName) {
        this.sectionName = sectionName;
    }

    public String getSectionName() {
        return sectionName;
    }

    public void setSectionName(String sectionName) {
        this.sectionName = sectionName;
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

}

数据类:

@ManagedBean(name = "NJData")
@SessionScoped
public class Data implements Serializable {
    private static final long serialVersionUID = 1L;

    private List<NJDataModel> dataModel;

    @PostConstruct
    public void init() {
        /** defaults */
        dataModel = new ArrayList<NJDataModel>();
        dataModel.add(new NJDataModel("Title page"));
    }

    public void upload() {
        System.out.println("upload method triggered");
        String msg = null;
        for (NJDataModel i : dataModel) {
            UploadedFile file = i.getFile();
            // ERROR: file is always null? so could not get hold of file
            if (file != null) {
                System.out.println("Uploaded file:" + file.getFileName());
                msg += "file:" + file.getFileName() + "size:" + file.getSize() + ", ";
            }
        }
        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(msg));
    }
}

XHTML:

<p:dataTable var="item" value="#{NJData.dataModel}"
    draggableColumns="true">
    <p:column>
        <p:panel>
            <f:facet name="header">
                <h:outputText value="#{item.sectionName}" />
            </f:facet>
            <p:fileUpload value="#{item.file}" mode="simple" />
        </p:panel>
    </p:column>
</p:dataTable>

<p:commandButton value="Submit" ajax="false"
    actionListener="#{NJData.upload}" />

最后,我需要在方法中获取文件:NJData.upload() - 我总是在哪里得到空值?

我的控制台输出:

file object:null & section name:first page
file object:null & section name:second page
4

0 回答 0