How to use PrimeFaces p:fileUpload? Listener method is never invoked or UploadedFile is null / throws an error / not usable

前端 未结 10 2359
孤独总比滥情好
孤独总比滥情好 2020-11-21 05:04

I\'m trying to upload a file using PrimeFaces, but the fileUploadListener method isn\'t being invoked after the upload finishes.

Here is the view:

相关标签:
10条回答
  • 2020-11-21 05:21

    You are using prettyfaces too? Then set dispatcher to FORWARD:

    <filter-mapping>
       <filter-name>PrimeFaces FileUpload Filter</filter-name>
       <servlet-name>Faces Servlet</servlet-name>
       <dispatcher>FORWARD</dispatcher>
    </filter-mapping>
    
    0 讨论(0)
  • 2020-11-21 05:21

    bean.xhtml

        <h:form enctype="multipart/form-data">    
    <p:outputLabel value="Choose your file" for="submissionFile" />
                    <p:fileUpload id="submissionFile"
                        value="#{bean.file}"
                        fileUploadListener="#{bean.uploadFile}" mode="advanced"
                        auto="true" dragDropSupport="false" update="messages"
                        sizeLimit="100000" fileLimit="1" allowTypes="/(\.|\/)(pdf)$/" />
    
    </h:form>
    

    Bean.java

    @ManagedBean
    

    @ViewScoped public class Submission implements Serializable {

    private UploadedFile file;
    
    //Gets
    //Sets
    
    public void uploadFasta(FileUploadEvent event) throws FileNotFoundException, IOException, InterruptedException {
    
        String content = IOUtils.toString(event.getFile().getInputstream(), "UTF-8");
    
        String filePath = PATH + "resources/submissions/" + nameOfMyFile + ".pdf";
    
        MyFileWriter.writeFile(filePath, content);
    
        FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO,
                event.getFile().getFileName() + " is uploaded.", null);
        FacesContext.getCurrentInstance().addMessage(null, message);
    
    }
    

    }

    web.xml

        <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</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>
        <servlet-name>Faces Servlet</servlet-name>
    </filter-mapping>
    
    0 讨论(0)
  • 2020-11-21 05:29

    With JBoss 7.2(Undertow) and PrimeFaces 6.0 org.primefaces.webapp.filter.FileUploadFilter should be removed from web.xml and context param file uploader should be set to native:

    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>native</param-value>
    </context-param>
    
    0 讨论(0)
  • 2020-11-21 05:33

    For people using Tomee or Tomcat and can't get it working, try to create context.xml in META-INF and add allowCasualMultipartParsing="true"

    <?xml version="1.0" encoding="UTF-8"?>
    <Context allowCasualMultipartParsing="true">
      <!-- empty or not depending your project -->
    </Context>
    
    0 讨论(0)
提交回复
热议问题