Primefaces FileUpload with PrettyFaces and JSF 2.2.3
Asked Answered
A

3

6

I'm having some problems with PrettyFaces on the pages I use <p:fileupload>.

When I disable the PrettyFaces filter on the page, everything works just fine. When I put it back, I can see the HTTP traffic on the webbrowser, but my fileupload handler is never fired.

This is some relevant part of the code.

Any help would be appreciated!

My .xhtml

<h:form id="formImportarSNs" enctype="multipart/form-data">  
<p:fieldset legend="Admin Panel">    
   <p:fileUpload value="#{adminPanelBean.file}" mode="simple"/>                            
   <p:commandButton ajax="false" actionListener="#{adminPanelBean.upload}"            value="confirm" />  

</p:fieldset>  
</h:form>  

My backingBean

package br.sgrd.bean;  

import javax.annotation.PostConstruct;  
import javax.annotation.PreDestroy;  
import javax.faces.application.FacesMessage;  
import javax.faces.bean.ManagedBean;  
import javax.faces.bean.ViewScoped;  
import javax.faces.context.FacesContext;  

import org.primefaces.model.UploadedFile;  

@ViewScoped  
@ManagedBean  
public class AdminPanelBean{  

    private UploadedFile file;  

    public void upload() {    
        if(arquivo != null) {    
            FacesMessage msg = new FacesMessage("Succesful", file.getFileName() + "     is uploaded.");    
            FacesContext.getCurrentInstance().addMessage(null, msg);    
        }    
    } 

    // getters/setters/etc

}  

My 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"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee     http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
    version="3.0">  
    <display-name>GRD</display-name>      

    <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>/faces/*</url-pattern>  
    </servlet-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>  
        <url-pattern>/*</url-pattern>  
        <servlet-name>Faces Servlet</servlet-name>  
        <dispatcher>FORWARD</dispatcher>  
    </filter-mapping>   



</web-app>  

My pretty-config.xml

<?xml version="1.0" encoding="UTF-8"?>  
<pretty-config xmlns="http://ocpsoft.org/schema/rewrite-config-prettyfaces"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://ocpsoft.org/schema/rewrite-config-prettyfaces  
                      http://ocpsoft.org/xml/ns/prettyfaces/rewrite-config-    prettyfaces.xsd">  

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

</pretty-config> 

Edit: Cleaned up some code.

Ab answered 10/12, 2013 at 10:31 Comment(0)
A
15

I've found the solution.

Create the context.xml in the META-INF folder and put this code:

<?xml version="1.0" encoding="UTF-8"?>
<Context allowCasualMultipartParsing="true">
</Context>

You can remove the Fileupload Filter from web.xml

Where I found the solution: http://ocpsoft.org/rewrite/docs/faq

And that's why you need to put it: How to use HttpServletRequest#getParts() in a servlet filter running on Tomcat?

If your problem is the size of the file, take a look at: <p:fileUpload> limiting to 2 Mb

Ab answered 16/12, 2013 at 17:53 Comment(3)
Does it work for file uploads above 2-3 MB file size ? In my case it works for small image uploads after adding your solution but not for larger file uploads.Geerts
Take a look at this question: #21257267Ab
Thanks.. it would better if you include this link in your answer to make it more complete,.Geerts
L
0

It does not work very well. Works with upload small files. If the file is a few MB he can not invoke the method. I do not advise using Primefaces + prettyfaces.

If your case is upload small files can work. Remove the primefaces fileupload configuration of web.xml and create the context.xml file as mentioned above.

I did with 'rewrite-servlet-2.0.9.Final.jar'. It worked perfectly with upload small files.

Lemur answered 25/11, 2014 at 11:51 Comment(0)
F
-1

Use in 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>
    <url-pattern>*.jsf</url-pattern> 
</filter-mapping>
Floats answered 12/1, 2015 at 6:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.