JSF 2.2 h:inputFile not working with pretty faces [duplicate]
Asked Answered
W

2

6

we're using a Glassfish 4.0 with JSF 2.2 (Mojarra 2.2.0) and PrettyFaces 2.0. When trying to upload a file using h:inputFile with the corresponding form enctype="multipart/form-data", the form action is only fired if the page is called directy, but nothing happens if the pretty url is called. Many other questions have some similar issues (e.g. How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null), but most of them seem to use PrimeFaces and have difficulties with the order of the filters etc. Since we want to keep the JSF method for uploading files, I'd like to know if there is a configuration of some filters of Mojarra that I might have missed.

The web.xml does currently not contain any filter specs.

the jsf file contains only this form

<h:form enctype="multipart/form-data">
   <h:inputFile value="#{fileModel.testFile}"/>
   <h:commandButton value="Upload" action="#{fileModel.upload}"/>
</h:form>

and the backing bean looks like this

@ApplicationScoped
@Named
public class FileModel {

    private Part testFile;

    public Part getTestFile() {
        return testFile;
    }

    public void setTestFile(Part testFile) {
        this.testFile = testFile;
    }

    public void upload() {
        System.out.println("File Data: " + testFile);
    }
}

then, uncommenting these lines in the pretty-config.xml will yield the error, while commenting them won't.

<url-mapping id="fileTest">
    <pattern value="/file" />
    <view-id value="/view/fileTest.xhtml" />
</url-mapping>

I think the problem might be described in this post by OCPSoft, but there doesn't seem to be a solution yet.

Willett answered 7/3, 2014 at 9:4 Comment(7)
The FAQ seems to cover your issue. AFAIK JSF 2.2 h:inputFile component uses Servlet 3.0 capabilities in order to perform the file upload. You'll need to check what kind of server is being hit in your application server in order to configure it in the web.xml (basically what your linked answer does, but changing the PF custom filter by the one used by you).Moreen
but the FAQ states 3rd party component, what I'm trying to use is the file input of the default implementation. Which filter would I have to configure?Willett
As the FAQ says it is necessary to enable any additional filters between PrettyFaces and JSF to listen to Servlet Forwards. You'll need to do some debugging work. Just check what kind of filter (it must be covered by Servlet 3.0 spec) is being hit when you upload a file. Anyway, it would be nice from you to provide a minimal version of the code in order for us to replay the error. Check the SSCCE format. Also specify your JSF 2.2 impl version (I suppose it is Mojarra 2.2.0).Moreen
Can you give me a hint how I could debug which filters are actually hit? Since the methods in my files are all ignored, I don't really know where to start...Willett
You could begin with a point at FacesServlet. Anyway, you haven't posted your web.xml here.Moreen
No, we currently use the primefaces fileupload, forcing it to use the commons filter rather than the one built-in. We didn't find an other solution yet.Willett
Probably you can find answer for your question here #22943566 by Kian.Workable
C
0

My suggestion is: page which is bookmarkable via pretty faces, should not contain any submit form! That kind of page should be only for view, move your file upload form to another regular jsf page without prettyfaces filter

Claimant answered 19/7, 2014 at 19:43 Comment(0)
J
-1

Just in case you want to do an implementation with primefaces, i already use an implementation like this

1.- Configure Filter´s 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>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

2.- Page implementation

<h:form id="form-file-upload" enctype="multipart/form-data">
                <p:fileUpload
                        auto="false"
                        mode="advanced"
                        value="#{yourBean.file}"
                        fileUploadListener="#{yourBean.fileListener}"
                        invalidSizeMessage="max size 10MB"
                        sizeLimit="10485760"/>


</h:form>

3.- Bean implementation

@ManagedBean
@ViewScoped
public class YourBean {

    private UploadedFile file;

    public UploadedFile getFile() {
        return file;
    }

    public void setFile(UploadedFile file) {
        this.file = file;
    }

    public void fileListener(FileUploadEvent e){
        this.file = e.getFile();
    }
}

ShowCase Primefaces

Jacktar answered 7/9, 2015 at 20:49 Comment(1)
This doesn't answer the current question. If you want to share code snippets, use Github or so. Future users having trouble with <p:fileUpload> can just read https://mcmap.net/q/17242/-how-to-use-primefaces-p-fileupload-listener-method-is-never-invoked-or-uploadedfile-is-null-throws-an-error-not-usable as already linked in top of question.Terpsichorean

© 2022 - 2024 — McMap. All rights reserved.