Jsf PrimeFaces简单文件上载:NullPointerException
我尝试制作一个简单的文件上载,以便:Jsf PrimeFaces简单文件上载:NullPointerException,jsf,file-upload,primefaces,nullpointerexception,Jsf,File Upload,Primefaces,Nullpointerexception,我尝试制作一个简单的文件上载,以便: <h:form enctype="multipart/form-data"> <p:fileUpload value="#{catastrofes.file}" mode="simple"></p:fileUpload> <p:separator/> <h:commandButton value="Dummy Action" action="#{catastrofes.dummyAct
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{catastrofes.file}" mode="simple"></p:fileUpload>
<p:separator/>
<h:commandButton value="Dummy Action" action="#{catastrofes.dummyAction}" ajax="false"></h:commandButton>
</h:form>
和my web.xml:
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>auto|native|commons</param-value>
</context-param>
<filter>
<filter-name>Primefaces FileUpload Filter</filter-name>
<filter-class>
org.primefaces.webapp.filter.FileUploadFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Primefaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
谢谢 根据primefaces\u用户指南 自动:这是默认模式,PrimeFaces通过检查 运行时环境,如果JSF运行时至少为2.2,则选择本机上载程序,否则选择commons。 本机:本机模式使用Servlet3.x部分API上载文件,如果JSF运行时小于2.2 正在抛出异常。 commons:此选项选择commons fileupload,而不考虑环境,具有以下优点: 这个选项是,它甚至可以在Servlet2.5环境中工作。 您应该只选择一个选项,而不是粘贴所有这三个选项。 如果您的项目中有commons fileupload库,我建议您选择commons 在web.xml中
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
你从来没有给你的文件赋值。对不起@Sybren,但我不明白,赋值是什么意思?你只声明文件,但不赋值,所以你的变量为null,你会得到一个null指针异常。尝试扩大你的支持bean的范围,可能是@ViewScoped
<context-param>
<param-name>primefaces.UPLOADER</param-name>
<param-value>commons</param-value>
</context-param>
<h:form enctype="multipart/form-data">
<p:growl id="messages" showDetail="true" />
<p:fileUpload value="#{fileUploadView.file}" mode="simple" />
<br /> <br />
<h:commandButton value="Dummy Action"
action="#{fileUploadView.upload}">
</h:commandButton>
</h:form>
@ManagedBean
@RequestScoped
public class FileUploadView {
private UploadedFile file;
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void upload() {
System.out.println("Uploaded File Name Is :: " +
file.getFileName() +
" :: Uploaded File Size :: " + file.getSize());
if(file != null) {
FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
}