简体   繁体   中英

primefaces JSF P:fileUpload cannot get the response

I am facing a problem with <p:fileUpload> of PrimeFaces. I created a Facelet page to upload the Excel file as below:

<p:fileUpload fileUploadListener="#{blackListImportBean.xlsFileUpload}"
    multiple="true" allowTypes="*.xls;*.xlsx" description="*.xls;*.xlsx" 
    sizeLimit="100000"/>
<h:commandButton actionListener="#{blackListImportBean.test}" 
    value="#{msg.SAVE}" action="test-page.xhtml" />

And bean java code as below:

public void xlsFileUpload(FileUploadEvent event){
    // ...
}

public void test() {
    // ...
}

When I click the button, the method test() is called and the method xlsFileUpload() is not invoked and an error prompts that it cannot find the method xlsFileUpload() , because the method need the parameter. When I remove the parameter, the page cannot find the method. Another issue which confused me is that I cannot get the upload file. I did it as per the documentation and I do not know what should I do.

Don't forget to add this in your 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>
</filter-mapping>

Two questions:

1) Are you using Primefaces 2.X or 3.X? 2) What is on the stack trace? It probably contains the information as to why.

The file-upload component uploads the file on its own event sequence so that will get triggered when the user triggers the file upload. This can be automatic via the property auto="true". Alternatively it displays an "upload" button that causes the upload. As such, it is separated from the second action which is your test method.

Judging from the fact it can't find your method I would guess that either bean is unmanaged or that your environments are out of sync (clean build).

Also, try a simple test:

@ViewScope
public class TestBean
{
  public void handleFileUpload(FileUploadEvent evt)
  {
     System.out.println("Handling Upload: " + evt.getFile());
     UploadedFile upload = evt.getFile();
FacesContext.getCurrentInstance()
                .addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "File Uploaded", "This file is " + upload));
     . . . //do whatever here....
  }
}

//JSF Page

. . .
     <h:form>
        <p:messages id="messages" />
        <p:fileUpload 
          fileUploadListener="#{testBean.handleFileUpload}"   
          multiple="true" 
          allowTypes="*.*;" 
          update="messages"
        />
      </h:form>

. . .

If your filter is set you should see a series of messages displayed for each file that's uploaded. If not, you should get a useful error message. Also, be aware you need a fair amount of basic Apache libraries (CommonsFileUpload) on the path and odds are that this is causing your problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM