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

守給你的承諾、 提交于 2019-12-08 20:33:22

问题


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.


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/22245691/jsf-2-2-hinputfile-not-working-with-pretty-faces

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!