简体   繁体   English

Primefaces FileUpload事件未触发 - JSF 2.0

[英]Primefaces FileUpload event not firing - JSF 2.0

I have posted my question on the Primefaces forum but no one has responded so I figured I would try here. 我在Primefaces论坛上发布了我的问题,但没有人回复,所以我想我会在这里试试。

I have been attempting to get fileUpload to work for some time now. 我一直试图让fileUpload工作一段时间。 I am currently running the RC2 build with mojarra 2.0.3 and Tomcat 7. 我目前正在使用mojarra 2.0.3和Tomcat 7运行RC2构建。

I have a dialog which will contain the fileUpload component like so. 我有一个对话框,其中包含fileUpload组件。

<p:dialog id="uploadFileDialog" >
   <h:form id="uplaodFileForm" prependId="false" enctype="multipart/form-data">
       <p:fileUpload fileUploadListener="#{fileUploadController.uploadFile} auto="true"/>    
   </h:form>
</p:dialog>

The fileUploadController looks like this fileUploadController看起来像这样

public class FileUploadController {
    public void uploadFile(FileUploadEvent event) {
         byte[] file = event.getFile().getContents();

         System.out.println("MADE IT INTO FILE UPLOAD !!! ");
    }
}

For some reason when the file is uploaded it never triggers the fileUploadEvent and it never gets into the controller. 由于某种原因,当文件上传时,它永远不会触发fileUploadEvent,它永远不会进入控制器。 The upload looks like its working, the flash part renders and gives the impression its doing something but no backing bean is ever being called. 上传看起来像它的工作,flash部分呈现并给人的印象是它做了什么,但没有调用任何支持bean。 I can seem to figure out what I am doing wrong and I have read just about every post on uploading a file using primefaces. 我似乎可以弄清楚我做错了什么,我已经阅读了关于使用primefaces上传文件的每篇文章。 Anyone know what I am doing wrong? 谁知道我做错了什么?

java.lang.ClassNotFoundException: org.apache.commons.io.output.DeferredFileOutputStream java.lang.ClassNotFoundException:org.apache.commons.io.output.DeferredFileOutputStream

PrimeFaces fileupload uses Apache Commons FileUpload under the covers which in turn has another dependency, the Apache Commons IO . PrimeFaces fileupload使用Apache Commons FileUpload ,后者又有另一个依赖项,即Apache Commons IO Ensure that you've both JAR's in your /WEB-INF/lib . 确保您的/WEB-INF/lib都有两个JAR。


Update : as per the comments, you also need to ensure that the upload filter is been declared in web.xml as per the users' guide : 更新 :根据评论,您还需要确保根据用户指南web.xml声明上传过滤器:

<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 you also need to ensure that there are no other filters before in the web.xml which may be reading the HttpServletRequest#getInputStream() , because it can be read only once. 而且你还需要确保web.xml 之前没有其他过滤器可能正在读取HttpServletRequest#getInputStream() ,因为它只能被读取一次。

that is correct you must add 这是正确的,你必须添加

<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 later this 以后这个

<!-- JSF mapping -->
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

aditionaly if your using maven add this dependencies aditionaly如果你使用maven添加这个依赖项

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.2.1</version>
</dependency>     
<dependency>
     <groupId>org.apache.commons</groupId>
     <artifactId>commons-io</artifactId>
     <version>1.3.2</version>
</dependency>
<dependency>
     <groupId>portlet-api</groupId>
     <artifactId>portlet-api</artifactId>
     <version>1.0</version>
</dependency>

I have also experienced a similar problem. 我也遇到了类似的问题。 The fix for me (using a Maven project) was to add the following dependencies in the pom.xml file: 我的修复(使用Maven项目)是在pom.xml文件中添加以下依赖项:

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.2.1</version>
    </dependency>

This is the equivalent of having the corresponding .jar files in your WEB-INF/lib, so try do that if this is not a Maven project. 这相当于在WEB-INF / lib中具有相应的.jar文件,因此如果这不是Maven项目,请尝试这样做。

i think i have solved your problem. 我想我已经解决了你的问题。 Check on your web.xml the presence of: 检查你的web.xml是否存在:

<context-param>
    <param-name>com.sun.faces.enableViewStateIdRendering</param-name>
    <param-value>false</param-value>
</context-param>

You must delete this option or set it do True (the default value). 您必须删除此选项或将其设置为True(默认值)。

I have the same issue, I have solved it by adding 我有同样的问题,我通过添加解决了它

<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>

like BalusC said. 像BalusC说的那样。

but adding this : 但添加这个:

<!-- JSF mapping -->
<servlet>
   <servlet-name>Faces Servlet</servlet-name>
   <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   <load-on-startup>1</load-on-startup>
</servlet>

<!-- Map these files with JSF -->
<servlet-mapping>
   <servlet-name>Faces Servlet</servlet-name>
   <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

Because by default in J2EE 6 this part is optional JSF 2.0 Servlet activates automatically when the WEB-INF/faces-config.xml descriptor is present. 因为在J2EE 6中默认这部分是可选的,所以当WEB-INF / faces-config.xml描述符存在时,JSF 2.0 Servlet会自动激活。

But it necessary to active correctly PrimeFaces Filter 但有必要正确激活PrimeFaces Filter

Jboss 6.1.0.Final / PrimeFaces 3.0.RC2 Jboss 6.1.0.Final / PrimeFaces 3.0.RC2

In Websphere 7 the event is fired because when I select file and press upload I can see bar to upload that grow up. 在Websphere 7中,事件被触发,因为当我选择文件并按上传时,我可以看到要上传的长大。 The problem is that in Websphere 7 I suppose that there are a filter that consume HttpRequest and when arrive to event listener is just consumed so don't have data :( 问题是在Websphere 7中,我认为有一个过滤器消耗HttpRequest,当到达事件监听器时刚刚消耗,所以没有数据:(

No message are present in log the debugging is very complicate. 日志中没有消息,调试非常复杂。 Exists some trace or logger to enable in JSF 2 Mojarra 2 and PrimeFaces 3.4.2? 在JSF 2 Mojarra 2和PrimeFaces 3.4.2中是否存在一些跟踪或记录器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM