PrimeFaces fileUpload事件不会触发

时间:2011-09-13 10:45:18

标签: java jsf primefaces

在我在Apache Tomcat 6上运行的应用程序中,我使用PrimeFaces 3.0.M3中的fileUpload。但它无法正常工作。当我上传文件时,它出现在上传目录中,但事件未被触发。有人可以帮帮我吗?

我的观看代码:

<h:form prependId="false" id="uploadForm" enctype="multipart/form-data">
    <p:fileUpload id="uploadControl" fileUploadListener="#{fileUploadBean.uploadHandler}" mode="advanced" />
</h:form>

FileUploadBean类源:

@ManagedBean
@RequestScoped
public class FileUploadBean implements Serializable {

    private static final Logger logger = Logger.getLogger(FileUploadBean.class);

    private UploadedFile file;

    public void uploadHandler(FileUploadEvent event) throws AbortProcessingException {
        logger.debug("uploadHandler(FileUploadEvent event)");
        logger.debug("Uploaded file: " + event.getFile().getFileName());
    }

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }
}

我的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_2_5.xsd" id="WebApp_ID" version="2.5">
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.THEME</param-name>
        <param-value>casablanca</param-value>
    </context-param>
    <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>/tmp</param-value>
        </init-param>
    </filter>
    <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>/*</url-pattern>
    </filter-mapping>
    <servlet>
        <servlet-name>Resource Servlet</servlet-name>
        <servlet-class>org.primefaces.resource.ResourceServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>Resource Servlet</servlet-name>
        <url-pattern>/primefaces_resource/*</url-pattern>
    </servlet-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>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>30</session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsf</welcome-file>
    </welcome-file-list>
    <login-config>
        <auth-method>BASIC</auth-method>
    </login-config>

    <servlet>
        <servlet-name>StartupServlet</servlet-name>
        <servlet-class>com.greytower.servlets.StartupServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <listener>
        <listener-class>com.project.servlets.ContextListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>LoginServlet</servlet-name>
        <servlet-class>com.project.servlets.LoginServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>LoginServlet</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>AjaxServlet</servlet-name>
        <servlet-class>com.project.servlets.AjaxServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>AjaxServlet</servlet-name>
        <url-pattern>/ajax</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>DownloadServlet</servlet-name>
        <servlet-class>com.project.servlets.DownloadServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>DownloadServlet</servlet-name>
        <url-pattern>/get</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>ImageServlet</servlet-name>
        <servlet-class>com.project.servlets.ImageServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>ImageServlet</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>
</web-app>

1 个答案:

答案 0 :(得分:3)

我认为您的问题出在web.xml配置中,您为文件上传过滤器声明了两个不同的过滤器映射,其中一个不正确:

<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>/*</url-pattern>
</filter-mapping>

相反,你需要这个:

 <filter-mapping>
   <filter-name>PrimeFaces FileUpload Filter</filter-name>
   <servlet-name>Faces Servlet</servlet-name>
</filter-mapping>

您只想将文件上传过滤器映射到Faces Servlet。