0

I using in my project PrimeFaces 5.3 with PrettyFaces 3.3.3. The problem what i got is upload file (ajax upload)

      <p:fileUpload 
         fileUploadListener="#{userProductsView.handlePrimaryImageUpload}"
         value="#{userProductsView.primaryImageTemp}" 
         process="@this"
         auto="true"
         mode="advanced"
         label="&nbsp;" /> 

and here is my prettyfaces configuration file (there is dynamic set view-id from manage bean)

<url-mapping id="global_login"> 
    <pattern value="/logowanie" /> 
    <view-id value="#{routesService.getViewPath}" />
</url-mapping>

and manage bean:

   @RequestScoped   // javax.enterprise.context.RequestScoped
   @Named("routesService")  // javax.inject.Named
   public class RoutesService {

          private Map<String, String> resources;

          public String getView(String pattern) {
              return resources.get(pattern);
          }

          public String getViewPath() {
                 String path = "/";
                 UrlMapping currentMapping = PrettyContext.getCurrentInstance().getCurrentMapping();
                 if (currentMapping != null) {
                     String pattern = currentMapping.getPattern();
                      if (pattern != null) {
                          String[] splited = pattern.split("/");
                          if (splited.length > 1) {
                              path = "/" + splited[1];
                          }
                      } 
                  }
                  return getView(path);
          }

   }

i investigate that. when i not use dynamic view-id (through routesService) value from bean for example:

  <url-mapping id="global_login"> 
    <pattern value="/logowanie" /> 
    <view-id value="/faces/logowanie.xhtml" />
  </url-mapping>

the upload works fine. I trying to resolve problem but i dont have Idea what i could do. Can anyone help with that issue i will grateful for any help.

below I include some peace of my pom.xml and web.xml

web.xml:

<context-param>
    <param-name>primefaces.UPLOADER</param-name>
    <param-value>commons</param-value>
</context-param>


<!-- SERVLET -->
<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>

<!-- FILTERS -->

<filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

<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>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

and pom.xml:

    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <dependency>
        <groupId>commons-io</groupId>
        <artifactId>commons-io</artifactId>
        <version>2.4</version>
    </dependency>

ACCORDING TO BalusC comment:

I set filters in the following order and both filters have pattern /*:

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

<filter>
    <filter-name>Pretty Filter</filter-name>
    <filter-class>com.ocpsoft.pretty.PrettyFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>Pretty Filter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>FORWARD</dispatcher>
    <dispatcher>REQUEST</dispatcher>
    <dispatcher>ERROR</dispatcher>
</filter-mapping>

in that configurations upload not working. (before it worked on PrettyFaces rewrited urls but only on that urls when mapping to view-id was hardcoded not injected from managed bean). I need to inject view-id url from managed bean so from that is this issue. Seems the problem happen when PrettyFaces engine when he inject the path from managed bean

Michał Ziembiński
  • 1,124
  • 2
  • 10
  • 31
  • What if you put the file upload filter mapping BEFORE the URL rewrite filter? See also "Troubleshooting" section of http://stackoverflow.com/q/8875818 – BalusC Mar 11 '16 at 09:15
  • yes i put the filter before prettyfilter and still not working. I think the problem appear in dynamic view-id. The upload not working only on urls where view-id path is loaded from managed bean – Michał Ziembiński Mar 11 '16 at 09:59
  • 1
    Remap the PrimeFaces upload filter to URL pattern of /* (while keeping it in front of PrettyFaces rewrite filter) and retry. This mapping shouldn't harm if you don't have any other upload handlers/servlets in same webapp. – BalusC Mar 11 '16 at 10:07
  • I update post accroding to your comment – Michał Ziembiński Mar 11 '16 at 10:55
  • 1
    Of course, remove that `FORWARD` dispatcher (and please read the abovementioned link to learn what exactly it means). In the future be careful with answers which don't explain anywhere how and why exactly the "answer" solves the problem. – BalusC Mar 11 '16 at 10:56
  • Thanks you very much!! You advice resolve the problem. Now working fine. like you said. I remove FORWARD dispatcher and now works. Thanks alot! – Michał Ziembiński Mar 11 '16 at 11:11

0 Answers0