环境 IDE:RAD 8.5 服务器:Websphere 8.5 Primefaces 3.5,JSF 2.0
@ManagedBean与@ViewScoped
我想创建一个页面,其中将添加多个附件,然后当用户点击添加时,所有附件都将添加到数据库中。处理文件上传并将上传的文件放入列表的方法为handleFileUpload
,此方法文件成功上传,因为println显示正确的文件名和其他详细信息。
假设向数据库添加附件的方法是addAttachmentAction()
,它会打印List is Empty
,因此上传的内容都会丢失。
编辑:相同的代码对@SessionScoped bean完全正常
请告诉我代码/方法有什么问题。完整代码如下所示
@ManagedBean
@ViewScoped
public class AttachmentBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = -8751473073670649932L;
private List<AttachedFileBO> attachedFileBOList;
private List<AttachedFileBO> deletedAttachedFileBOList;
private AttachedFileBO selectedAttachedFileBO;
private StreamedContent attachedFileStreamContent;
//getter setter for above declared field
.
public void handleFileUpload(FileUploadEvent event) {
try {
InputStream tempInputStream;
tempInputStream = event.getFile().getInputstream();
attachedFileBO attachedFileBO = new AttachedFileBO();
byte[] bytes;
bytes = IOUtils.toByteArray(tempInputStream);
attachedFileBO.setAttachedFile(bytes);
attachedFileBO.setAttachedFileContentType(event.getFile().getContentType());
attachedFileBO.setAttachedFileName(event.getFile().getFileName());
attachedFileBO.setDbAction("I");
Random random = new Random();
attachedFileBO.setAttachedFileTableId(random.nextInt());
// If file is already attached then add attached file to the list
if (getAttachedFileBOList() != null && !getAttachedFileBOList().isEmpty()) {
getAttachedFileBOList().add(attachedFileBO);
} else {
List<AttachedFileBO> tempList = new ArrayList<AttachedFileBO>();
tempList.add(attachedFileBO);
setAttachedFileBOList(tempList);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//After upload when I try to access the list of attachment then method below prints List is Empty
public void addAttachmentAction() {
if (getBusinessProcessAttachedFileBOList().isEmpty()) {
System.out.println("List is Empty");
} else {
logger.debug("List is NOT Empty");
}
//Rest of the coding
.
.
}
}
AttachedFileBO
public class AttachedFileBO implements Serializable {
/**
*
*/
private static final long serialVersionUID = 3886993061934034729L;
private int attachedFileTableId;
private int businessProcessId;
private byte[] attachedFile;
private String attachedFileName;
private String attachedFileContentType;
private String attachedFileExtension;
private String dbAction;
//Setter getter
.
.
}