2013-12-19 1 views
1

Я портировал рабочее приложение Primefaces JSF 2 из весенней конфигурации XML в новую Spring 3.2. Модель конфигурации Java. Так что в то же время я решил также портировать конфигурацию web.xml. Все прошло неплохо, но я, похоже, застрял в одной конкретной вещи. У меня есть вопрос о том, как установить init parms для фильтра в классе, реализующем WebApplicationInitializer.Как настроить основное лицо FileUploadFilter с помощью 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> 

В моей ApplicationInitializer, который реализует WebApplicationInitializer, я определил фильтр. Но я не могу понять, как установить init parms для thresholdSize и uploadDirectory.
Есть ли простой способ сделать это?

Thanks

ответ

2

Хорошо, я понял. Я создал собственную реализацию интерфейса FilterConfig и после того, как создал фильтр и передал его через метод init в фильтре.

Для тех, кто заинтересован здесь код

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; 
} 
+0

Это будет большой ответ, если ваша реализация CustomFilterConfig была опубликована вместе с ним. –

+0

В Spring есть класс под названием MockFilterConfig.java, который реализует FilterConfig, который даст хорошую отправную точку для тех, кто пытается создать свой собственный фильтр FilterConfig: https://github.com/spring-projects/spring-framework/blob/master/ spring-test/src/main/java/org/springframework/mock/web/MockFilterConfig.java –

+0

Забыл об этом. Мне придется вернуться и найти этот код. Я уволил этот проект когда-то прошлым летом. – EvilJinious1

Смежные вопросы