JSF dataTable和fileUpload(文件上载问题)

时间:2013-04-24 07:13:21

标签: jsf file-upload datatable javabeans

请告诉我你的建议。我想用特定的设计构建fileUpload,并且在获取uploadedFile时遇到问题。

我的设计:

enter image description here

DataModel类:

@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() - 我总是得到null?

我的控制台输出:

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

0 个答案:

没有答案