PrimeFaces 上传文件未上传
我正在使用 PrimeFaces 3 并尝试上传文件,但当我调试时,文件始终为空。 下面你可以看到我的代码。任何人都可以发现问题所在吗?
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{uploadFileMB.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false" action="#{uploadFileMB.submit()}"/>
<h:outputLabel value="#{uploadFileMB.text}" />
</h:form>
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.model.UploadedFile;
@ManagedBean
@SessionScoped
public class UploadFileMB {
UploadedFile file;
String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void submit(){
System.out.println("Trial "+file);
UploadedFile a=file;
if(file==null)
text="not uploaded";
else
text=file.getFileName()+" uploaded";
}
/** Creates a new instance of UploadFileMB */
public UploadFileMB() {
}
}
<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>
以及 web.xml 和 faces.config 中的过滤器 我尝试了很多建议并调试了很多次,但我无法弄清楚。
这是我的面孔配置:
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId> commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId> commons-io</artifactId>
<version>2.1</version>
</dependency>
<lifecycle>
<phase-listener>security.SecurityFilter</phase-listener>
</lifecycle>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:/home/vanessa/Desktop</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</faces-config>
I am using PrimeFaces 3 and trying to upload a file but when I debug the file is always null.
Below you can see my code. Can anyone spot what is the issue?
<h:form enctype="multipart/form-data">
<p:fileUpload value="#{uploadFileMB.file}" mode="simple" />
<p:commandButton value="Submit" ajax="false" action="#{uploadFileMB.submit()}"/>
<h:outputLabel value="#{uploadFileMB.text}" />
</h:form>
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import org.primefaces.model.UploadedFile;
@ManagedBean
@SessionScoped
public class UploadFileMB {
UploadedFile file;
String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public UploadedFile getFile() {
return file;
}
public void setFile(UploadedFile file) {
this.file = file;
}
public void submit(){
System.out.println("Trial "+file);
UploadedFile a=file;
if(file==null)
text="not uploaded";
else
text=file.getFileName()+" uploaded";
}
/** Creates a new instance of UploadFileMB */
public UploadFileMB() {
}
}
<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>
And the filter in both web.xml and faces.config
I have tried a number of suggestions and debugged it many times but I can't figure it out.
This is my faces-config:
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="2.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId> commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId> commons-io</artifactId>
<version>2.1</version>
</dependency>
<lifecycle>
<phase-listener>security.SecurityFilter</phase-listener>
</lifecycle>
<filter>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
<init-param>
<param-name>uploadDirectory</param-name>
<param-value>C:/home/vanessa/Desktop</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>PrimeFaces FileUpload Filter</filter-name>
<servlet-name>Faces Servlet</servlet-name>
</filter-mapping>
</faces-config>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的项目中缺少这两个库; commons-fileupload 和 commons-io 。如果你的项目是maven,你可以将这些添加到你的pom.xml中;
否则,请从 http://commons.apache.org 下载它们并添加您的 lib 。
I think these two libraries are missing in your project ; commons-fileupload and commons-io . if your project is maven you can add these to your pom.xml ;
else then download them from http://commons.apache.org and add your lib .
好吧,我在您的代码中看到三个错误,它可以解决您的问题,但我不保证任何事情。
首先,您从错误的包导入
@SessionScope
,它应该是javax.faces.bean.SessionScoped
,另一个类是与CDI
一起使用的代码>.其次,将你的属性设置为bean
private
我不确定它是否算作这样的属性。这也是尽可能隐藏字段的好习惯。第三,也是最重要的,将
action
更改为actionListener
并尝试一下。如果仍然不起作用,请尝试添加到您的方法参数ActionEvent event
(并小心选择正确的包,我曾经从javax.awt 导入
并花两个小时找出问题所在:-)ActionEvent
.Well, I see three mistakes in your code, it could solve your problem I don't guarantee anything.
First, you are importing
@SessionScope
from wrong package, it should bejavax.faces.bean.SessionScoped
, the other class is ment to use withCDI
.Second, make your properties in bean
private
I am not sure if it counts as a property like this. Also this is a good practise to hiding field as much as possible.Third, and most important, change
action
toactionListener
and try it. If still doesn't work, try to add to your method parameterActionEvent event
(and be carefull to choose right package, I once importedActionEvent
fromjavax.awt.
and spend two hours figuring out where could be the problem:-)