PrimeFaces 上传文件未上传

发布于 2025-01-02 23:55:03 字数 3378 浏览 0 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绝對不後悔。 2025-01-09 23:55:03

我认为您的项目中缺少这两个库; commons-fileupload 和 commons-io 。如果你的项目是maven,你可以将这些添加到你的pom.xml中;

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId> commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId> commons-io</artifactId>
            <version>2.1</version>
        </dependency>

否则,请从 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 ;

        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId> commons-fileupload</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>commons-io</groupId>
            <artifactId> commons-io</artifactId>
            <version>2.1</version>
        </dependency>

else then download them from http://commons.apache.org and add your lib .

小镇女孩 2025-01-09 23:55:03

好吧,我在您的代码中看到三个错误,它可以解决您的问题,但我不保证任何事情。

首先,您从错误的包导入@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 be javax.faces.bean.SessionScoped, the other class is ment to use with CDI.

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 actionto actionListenerand try it. If still doesn't work, try to add to your method parameter ActionEvent event (and be carefull to choose right package, I once imported ActionEventfrom javax.awt. and spend two hours figuring out where could be the problem:-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文