Primefaces 5 fileUpload处理程序未调用[重复]

Posted

技术标签:

【中文标题】Primefaces 5 fileUpload处理程序未调用[重复]【英文标题】:Primefaces 5 fileUpload handler not called [duplicate] 【发布时间】:2014-08-23 09:52:23 【问题描述】:

我正在使用 JSF 和 PrimeFaces 5.0 开发一个 Web 项目。虽然有很多关于这个问题的论坛,但我找不到 FileUpload 的解决方案。问题是,当我上传文件处理程序方法时,永远不会调用

这是我的代码:

HTML 页面:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" 
xmlns:ui="http://java.sun.com/jsf/facelets" 
xmlns:h="http://java.sun.com/jsf/html" 
xmlns:f="http://java.sun.com/jsf/core" 
xmlns:p="http://primefaces.org/ui">
<ui:composition template="/WEB-INF/templates/template.xhtml">

    <ui:define name="content">
        <f:view>
            <p:panel id="panel" header="Registros de Fuente" style="width:100%;">

                <h:form enctype="multipart-data">
                    <p:fileUpload fileUploadListener="#registrosBean.handleFileUpload"
                                    required="true" mode="advanced" dragDropSupport="false"
                                    multiple="true" sizeLimit="1000000" fileLimit="5" update="messages"
                                    allowTypes="/(\.|\/)(gif|jpe?g|png|pdf|doc|docx)$/">
                    </p:fileUpload>
                </h:form>

                <h:form enctype="multipart/form-data">
                    <p:fileUpload value="#registrosBean.file" mode="simple" />
                    <p:commandButton value="Submit" ajax="false"/>
                </h:form>
            </p:panel>
        </f:view>
    </ui:define>
</ui:composition>
</html>

豆:

@Component("registrosBean")
@ViewScoped
@ManagedBean
public class RegistroBean extends GenericBean<Registrofuente> implements Serializable 

    public void handleFileUpload(FileUploadEvent event) 
        System.out.println("SOME CONSOLE MESSAGE");
    

    public void cogerArchivos( ) 
        System.out.println("SOME CONSOLE MESSAGE");
    

    public UploadedFile file;

    /**
     * @return the file
     */
    public UploadedFile getFile() 
        return file;
    

    /**
     * @param file the file to set
     */
    public void setFile(UploadedFile file) 
        System.out.println("SOME CONSOLE MESSAGE");
        this.file = file;
    

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="3.0" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <display-name>UNAC</display-name>
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:application-context.xml</param-value>
  </context-param>
  <context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>aristo</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>
  <welcome-file-list>
    <welcome-file>pages/login.jsf</welcome-file>
  </welcome-file-list>
  <context-param>
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
  </context-param>
  <context-param>
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
    <param-value>resources.application</param-value>
  </context-param>
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>10240</param-value> <!-- 10 Mb -->
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </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>

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

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
</web-app>

关于我的项目的一些附加信息

JSF 2.1 IDE:Eclipse Jboss 7.1

重要的是,我已经将 commons-io 和 commons-fileupload 库添加到了 WEB-INF\lib 文件夹中

我已经很多天了,并就这个问题咨询了很多论坛,但我找不到解决方案,任何建议都会有所帮助。谢谢

【问题讨论】:

为什么文件上传里面有咆哮声? 我刚刚删除了咆哮信息,谢谢 可能和使用spring有关?移除@Component,并正确重写fileUploadListener属性中的EL。 我刚刚删除了@Component注释并没有发生任何事情,我将注释更改为:@ViewScoped@ManagedBean(name = "registrosBean") 【参考方案1】:

首先尝试简化 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>

而且您不需要那些在 WEB-INF\lib 上的 commons-io 库,因为您已经在使用 PF5。删除它们。

【讨论】:

以上是关于Primefaces 5 fileUpload处理程序未调用[重复]的主要内容,如果未能解决你的问题,请参考以下文章

PrimeFaces p:fileUpload 不调用方法

刷新后PrimeFaces fileUploader不可见

Primefaces fileUpload fileUploadListener问题

Primefaces:fileUpload显示Facesmessage无法正常工作

p:fileUpload 中的监听器方法永远不会在 primefaces 中调用 [重复]

如何在 PrimeFaces fileUpload 中向 fileUploadListener 发送参数