【问题标题】:PrimeFaces Upload file not uploadingPrimeFaces 上传文件未上传
【发布时间】:2012-02-05 11:22:11
【问题描述】:

我正在使用 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>

【问题讨论】:

    标签: upload primefaces


    【解决方案1】:

    我认为您的项目中缺少这两个库; 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 下载它们并添加您的库。

    【讨论】:

    • 谢谢。但它不起作用。我没有maven所以我刚刚添加了jar和你上面写的代码,但它仍然不起作用。其他建议?再次感谢!
    • 你的托管 bean 应该实现 Serializable ,可能是纠正你的错误
    • 谢谢。我添加了“实现可序列化”,但它仍然不起作用。还有其他想法吗?我已经在我的问题中添加了我的 faces-config。
    • 你应该从你的 faces-config 中删除 标签,因为它们在 pom.xml 中使用,这对于 maven 项目是特殊的。我在 filter params 看到了上传目录。删除这个参数后可以重新上传吗?
    【解决方案2】:

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

    首先,你从错误的包中导入@SessionScope,它应该是javax.faces.bean.SessionScoped,另一个类与CDI一起使用。

    其次,在 bean private 中创建你的属性我不确定它是否算作这样的属性。这也是尽可能隐藏字段的好习惯。

    第三,也是最重要的,将action更改为actionListener并尝试一下。如果仍然不起作用,请尝试添加到您的方法参数 ActionEvent event (并注意选择正确的包,我曾经从 javax.awt. 导入 ActionEvent 并花了两个小时找出问题所在:-)

    【讨论】:

      猜你喜欢
      • 2013-03-10
      • 1970-01-01
      • 2013-06-04
      • 1970-01-01
      • 2015-07-16
      • 2014-04-22
      • 1970-01-01
      • 2013-07-10
      • 2012-01-21
      相关资源
      最近更新 更多