简体   繁体   English

fileUpload不会使用JSF 2.2在PrimeFaces 3.5中触发事件

[英]fileUpload doesn't fire event in PrimeFaces 3.5 with JSF 2.2

I can't make fileUpload component on PrimeFaces 3.5 to fire the event. 我无法在PrimeFaces 3.5上创建fileUpload组件来触发事件。 I have read many posts about that topic and followed advise there but still it doesn't work. 我已经阅读了很多关于该主题的帖子,并在那里提出建议,但仍然无效。 I tried all of the modes (simple, auto, advanced) with no success. 我尝试了所有模式(简单,自动,高级)但没有成功。

If I use standard inputFile tag from the JSF specification it works properly. 如果我使用JSF规范中的标准inputFile标记,它可以正常工作。

This is my web.xml file: 这是我的web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>
<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>redmond</param-value>
</context-param>
<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>               
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>/faces/*</url-pattern>
</servlet-mapping>

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

<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>faces/home.xhtml</welcome-file>
</welcome-file-list>  
</web-app>

And this is part of my view page with the fileUpload tag: 这是我的视图页面的一部分,其中包含fileUpload标记:

<h:form enctype="multipart/form-data"> 
   <p:dialog id="basicDialog" header="Add pictures" widgetVar="dlg1" >  
       <p:fileUpload fileUploadListener="#{galleryManagedBean.addPicturesToGallery}" multiple="true"/>
   </p:dialog> 
</h:form>

The extract from managed bean with the method that is called from the tag: 使用从标记调用的方法从托管bean中提取:

@Named(value = "galleryManagedBean")
@RequestScoped
public class GalleryManagedBean {
    public void addPicturesToGallery(FileUploadEvent event)
    {
        System.out.println("Triggered upload");
    }
}

Also I would like to add that the Http POST request is fired properly after I checked it using the debugger tool in Chrome. 另外我想补充一点,在我使用Chrome中的调试工具检查后,Http POST请求被正确激活。

I have added necessary libraries to the classpath ie : 我已经在类路径中添加了必要的库,即:

commons-fileupload-1.3.jar
commons-io-2.4.jar 

This is caused by a change in FacesServlet of JSF 2.2. 这是由JSF 2.2的FacesServlet的更改引起的。 Since that version, FacesServlet natively supports file uploads (specifically: multipart/form-data HTTP requests) thanks to the presence of the new Servlet 3.0 specific @MultipartConfig annotation. 从那个版本开始,由于存在新的Servlet 3.0特定的@MultipartConfig注释, FacesServlet本身支持文件上传(特别是: multipart/form-data HTTP请求)。 Also, a new <h:inputFile> component was been introduced to offer a file upload component in the standard JSF component set. 此外,还引入了一个新的<h:inputFile>组件,以在标准JSF组件集中提供文件上载组件。

This all conflicts with PrimeFaces file upload facility in older PrimeFaces 3.x versions which didn't take this new JSF 2.2 feature into account at all. 这一切都与旧版PrimeFaces 3.x版本中的PrimeFaces文件上传工具发生冲突,而这些版本并没有考虑到这个新的JSF 2.2特性。 The PrimeFaces 3.x file upload filter parsed and consumed the entire request while it should leave this job up to the FacesServlet . PrimeFaces 3.x文件上传过滤器解析并使用整个请求,同时应将此作业留给FacesServlet This caused the FacesServlet to be unable to properly decode the HTTP request (determining the submitted values and actions). 这导致FacesServlet无法正确解码HTTP请求(确定提交的值和操作)。

PrimeFaces 4.0, which is designed specifically for JSF 2.2, has taken this all into account. 专为JSF 2.2设计的PrimeFaces 4.0已将这一切考虑在内。 In this changeset of the PrimeFaces file upload filter you can see the changes done to recognize JSF 2.2 and bypass the parsing in the filter. 在PrimeFaces文件上传过滤器的此变更集中,您可以看到为识别JSF 2.2所做的更改并绕过过滤器中的解析。 Theoretically, it should also suffice to entirely remove the file upload filter registration from web.xml so that this isn't used anymore. 理论上,它也应该足以从web.xml完全删除文件上载过滤器注册,以便不再使用它。

It should work fine if you upgrade to PrimeFaces 4.0. 如果升级到PrimeFaces 4.0,它应该可以正常工作。 It has coincidentally been officially released just 2 days ago, so you're pretty on time for that. 它恰好在2天前正式发布,所以你准时就可以了。

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

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