Primefaces FileUploadEvent 侦听器方法不起作用

Posted

技术标签:

【中文标题】Primefaces FileUploadEvent 侦听器方法不起作用【英文标题】:Primefaces FileUploadEvent Listener Method does not work 【发布时间】:2013-02-07 19:53:31 【问题描述】:

现在我在我的应用程序中使用Primefacs 3.3.1JSF 2.0。现在我面临一个问题,primefaces FileUploadEvent listener 即使表单提交也不起作用。 我的日志文件中没有错误和异常。但是,它不起作用。我缺少什么,lib 或配置?顺便说一句,我使用JBoss 7.1.1 应用服务器。

primefaces-3.3.1.jar
commons-io-1.4.jar
commons-fileupload-1.2.1.jar
poi-3.7.jar

mypages.xhtml

<h:form enctype="multipart/form-data">            
    <p:fileUpload fileUploadListener="#FileUploadController.handleFileUpload"  
            mode="advanced"  
            update="messages"   
            multiple="true"  
            sizeLimit="100000"   
            allowTypes="/(\.|\/)(gif|jpe?g|png)$/"/>  
    <p:commandButton value="Submit" ajax="false" actionListener="#FileUploadController.upload"/>  
    <!-- or -->
    <p:commandButton value="Submit" ajax="false" action="#FileUploadController.upload"/>  
</h:form>   

文件上传控制器.java

@Scope(ScopeType.CONVERSATION)
@Name("FileUploadController")
public class FileUploadController 
    private List<UploadedFile> uploadedFileList = new ArrayList<UploadedFile>();

    public void handleFileUpload(FileUploadEvent event) 
        System.out.println("handleFileUpload()....");
        UploadedFile file = event.getFile();
        uploadedFileList.add(file);
        FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");  
        FacesContext.getCurrentInstance().addMessage(null, msg);  
      

    public void upload() 
        System.out.println("Number of Uploaded Files : " + uploadedFileList.size());
    

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    <filter-mapping>
       <filter-name>PrimeFaces FileUpload Filter</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    <filter-mapping>
       <filter-name>PrimeFaces FileUpload Filter</filter-name>
       <url-pattern>Faces Servlet</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>#GuestPreferences.theme</param-value>
    </context-param>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.seam</url-pattern>
    </servlet-mapping>
    <listener>
        <listener-class>org.jboss.seam.servlet.SeamListener</listener-class>
    </listener>
    <filter>
        <filter-name>Seam Multipart Filter</filter-name>
        <filter-class>org.jboss.seam.web.MultipartFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Seam Multipart Filter</filter-name>
        <url-pattern>*.seam</url-pattern>
    </filter-mapping>
    <filter>
        <filter-name>Seam Filter</filter-name>
        <filter-class>org.jboss.seam.servlet.SeamFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>Seam Filter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <servlet-class>org.jboss.seam.servlet.SeamResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Seam Resource Servlet</servlet-name>
        <url-pattern>/seam/resource/*</url-pattern>
    </servlet-mapping>
    <context-param>
        <param-name>facelets.DEVELOPMENT</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring-beans.xml</param-value>
    </context-param>
    <security-constraint>
        <display-name>Restrict raw XHTML Documents</display-name>
        <web-resource-collection>
            <web-resource-name>XHTML</web-resource-name>
            <url-pattern>*.xhtml</url-pattern>
        </web-resource-collection>
        <auth-constraint />
    </security-constraint>
</web-app>

【问题讨论】:

移除接缝并重试。 @BalusC,如果我删除了Seam,我还能在我的应用程序中使用Seam吗? 只是为了排除一个和另一个 :) 你有一个 Seam 多部分过滤器,它表明它还将 HTTP 请求解析为multipart/form-data。但是一个 HTTP 请求只能被解析一次。 @BalusC,即使我删除了Seam Multipart Filter,它也不会调用侦听器方法。顺便说一句,使用 ajax 请求上传需要做什么?你能提供任何解决方案吗?感谢您的时间和提供。 好了,开始调试 HTTP 流量和 HTTP 请求正文解析。 【参考方案1】:

您应该禁用接缝多部分过滤器。你应该写

web:multipart-filter disabled="true"/> 在 components.xml 中。对我来说很好。

【讨论】:

以上是关于Primefaces FileUploadEvent 侦听器方法不起作用的主要内容,如果未能解决你的问题,请参考以下文章

实现PrimeFaces扩展时出错

使用ajax从jsf中的primefaces树选定项设置primefaces selectOneMenu默认值

JSF 2 PrimeFaces 安装

在 PrimeFaces 中添加 Angular Js - JSF

IBM Bluemix 支持 Primefaces? [关闭]

Primefaces 6.0 对话框框架和框架集