All posts tagged web.xml

  • Using Prettyfaces With Primefaces Upload

     

    <h:form id="upload" enctype="multipart/form-data">           
        <p:fileUpload fileUploadListener="#{fileUploadBean.handleFileUpload}" /> 
    </h:form>

    Well to get this to work smoothly with prettyfaces you must play with the configuration a bit.

    First in order to make sure that the primefaces upload works, you must make sure that you have these:

    • commons-fileupload and commons-io are present in the webapp’s runtime classpath (just dropping the JARs in /WEB-INF/lib ought to be sufficient).
    • The FileUploadFilter is mapped on the exact <servlet-name> of the FacesServlet as is been definied in your web.xml. If you’ve given it a <servlet-name> of for examplefacesServlet, then you need to edit it in the <filter-mapping> example as well.
    • The enctype of the <h:form> needs to be set to multipart/form-data.
    • In simple file upload with mode="simple", ajax must be disabled on any PrimeFaces command buttons/links by ajax="false".

    Now in the web.xml configure the fileupload filter which parses the multipart request. FileUpload filter should map to Faces Servlet:

    <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>
    <!-- Make sure that your servlet-name is the same in primefaces <filter-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>/faces/*</url-pattern>
    </servlet-mapping>

    Notice that I didn’t configure any related Prettyfaces servlet filter!
    If you are using a Servlet 3.0 compliant container, PrettyFaces will automatically register the required Servlet Filter; otherwise, make sure PrettyFilter is the first filter in your web.xml file. (The dispatcher elements are required to ensure PrettyFaces intercepts both internal and external requests.)

    The trick in the config is the dispatcher inside filter-mapping of the primefaces upload filter

    <dispatcher>FORWARD</dispatcher>

    That’s it, you should now be able to upload with Prettyfaces URLs.

    Case built with:
    Primefaces 3.5
    prettyfaces 2.0.4
    commons-io 2.4
    commons-fileupload 1.3

    A helpful stackoverflow post