0

あなたの提案を教えてください。特定の設計で fileUpload を構築したいのですが、uploadedFile の取得に問題があります。

私のデザイン:

ここに画像の説明を入力

データモデル クラス:

@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
4

0 に答える 0