环境:
在选择文件后单击按钮时,文件var为空。
Test.xhtml
<!DOCTYPE html>
<html lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<f:view contentType="text/html">
<h:head>
<title>Application</title>
</h:head>
<h:body>
<h:form id="frmTest" enctype="multipart/form-data">
<ui:include src="template/messages.xhtml" />
<p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="false"
update="messages" sizeLimit="100000" fileLimit="3" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
</h:form>
</h:body>
</f:view>
</html>
TestBean.java
@ViewScoped
@ManagedBean
public class TestBean implements Serializable {
...
public void handleFileUpload(FileUploadEvent event) {
UploadedFile file = event.getFile();//file is null
FacesUtils.addInfoMessage("Succesful" + file.getFileName() + " is uploaded.");
System.out.println("Succesful" + event.getFile().getFileName() + " is uploaded.");
}
...
答案 0 :(得分:-1)
您应该从update=""
组件中删除或填充fileUpload
属性。它在上传后将文件“重定向”到组件。