1

我一直在将工作的 Primefaces JSF 2 应用程序从 spring XML 配置移植到更新的 Spring 3.2 Java 配置模型。所以同时我决定移植 web.xml 配置。事情进展得很顺利,但我似乎被一件特别的事情困住了。我的根本问题是如何在实现 WebApplicationInitializer 的类中为过滤器设置初始化参数。

所以我有以下 web.xml 部分

 <filter>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <filter-class>
        org.primefaces.webapp.filter.FileUploadFilter
    </filter-class>
    <init-param>
        <!-- we set the threshold size to be exactly 1 megabyte -->
        <param-name>thresholdSize</param-name>
        <param-value>1048576</param-value>
    </init-param>
    <init-param>
        <!-- this is the location for the upload  -->
        <param-name>uploadDirectory</param-name>
        <!-- we select the system tmp directory for this -->
        <param-value>/tmp/myapp/uploads</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>PrimeFaces FileUpload Filter</filter-name>
    <servlet-name>facesServlet</servlet-name>
</filter-mapping>

在实现 WebApplicationInitializer 的 ApplicationInitializer 中,我定义了一个过滤器。但我看不到如何为 thresholdSize 和 uploadDirectory 设置初始化参数。
有没有一种简单的方法可以做到这一点?

谢谢

4

1 回答 1

2

Ok I figured it out. I created my own implementation of the FilterConfig interface and after I created the filter and passed it in through the init method on the filter.

For those interested here is the code

 private void setupFileUploadFilter(ServletContext container) {
    try {
        // create the filter config for the file upload filter
        FilterConfig filterConfig = setupFileUpLoadFilterConfig(container);

        // create the filter
        FileUploadFilter fileUploadFilter = new FileUploadFilter();

        // initialize the file upload filter with the specified filter config
        fileUploadFilter.init(filterConfig);

        // register the filter to the main container
        FilterRegistration.Dynamic fileUploadFilterReg = container.addFilter("PrimeFaces FileUpload Filter", fileUploadFilter);

        // map it for all patterns
        fileUploadFilterReg.addMappingForUrlPatterns(null, false, "/*");

        // add a mapping to the faces Servlet
        fileUploadFilterReg.addMappingForServletNames(null, false, "facesServlet");

    } catch (ServletException e) {
        e.printStackTrace();
    }
}

/**
 * create the initialization parameters for the file upload filter
 *
 * @param container the main container
 * @return the created filter config
 */
private FilterConfig setupFileUpLoadFilterConfig(ServletContext container) {
    CustomFilterConfig filterConfig = new CustomFilterConfig(container, "PrimeFaces FileUpload Filter");

    // add the size parameter which is 1 Megabyte
    filterConfig.addInitParameter("thresholdSize", "1048576");

    // add the size parameter
    filterConfig.addInitParameter("uploadDirectory", "/tmp/myapp/uploads");

    return filterConfig;
}
于 2014-01-01T19:42:43.807 回答