Scenario: I have a p:fileupload multiple="true" and a p:datatable below that. When I upload a file, the filename and filesize should be added in an ArrayList and should be displayed in the datatable. And for each file being attached, the arrayList should get appended with a new row.
Problem: When I upload the first file, the datatable is displaying the file details. But when I upload the 2nd file, the 2nd file's details are only shown and previous file details get deleted. Seems like the array list is getting reset. To be specific only the last attached file details are displayed.
My code below:
Fileupload.xhtml
<p:fileUpload fileUploadListener="#{f.uploadTemp}" mode="advanced"
update="fileTable" multiple="true" />
<p:dataTable value="#{f.fileList }" var="file" id="fileTable">
<p:column headerText="FileName">
<p:commandLink id="ajax" actionListener="#{f.dowload}">
<h:outputText value="#{file.fileName }" />
</p:commandLink>
</p:column>
<p:column headerText="FileSize">
<h:outputText value="#{file.fileSize }" />
</p:column>
<p:column>
<p:commandButton value="X">
<f:setPropertyActionListener target="#{file.fileName}" value="#{f.filetobeDeleted }"/>
</p:commandButton>
</p:column>
</p:dataTable>
Bean
@ManagedBean(name = "f")
@ConversationScoped
public class FileUploadBF implements Serializable {
private ArrayList<FileCarry> fileList = new ArrayList<FileCarry>();
public void uploadTemp(FileUploadEvent event) {
uploadedFile = event.getFile();
System.out.println("process upload");
FileCarry fc = new FileCarry(uploadedFile.getFileName(), uploadedFile.getSize());
this.fileList.add(fc);
}
public void setFileList(List<FileCarry> fileList) {
this.fileList = (ArrayList<FileCarry>) fileList;
}
public ArrayList<FileCarry> getFileList() {
return fileList;
}
public static class FileCarry {
public FileCarry(String name, long l) {
this.fileName = name;
this.fileSize = l;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public String getFileName() {
return fileName;
}
public void setFileSize(long fileSize) {
this.fileSize = fileSize;
}
public long getFileSize() {
return fileSize;
}
private String fileName;
private long fileSize;
}
}
Solution: It should be in view scope. But could some one tell why it does not work in coversation scope?
In my opinion you can have a problem with the scope of your bean. Did you start a conversation somewhere else? Check if your bean is created again and again?
You can change scope of your bean on for example: @ViewScoped - it should help.