永远不会调用fileUploadListener(primefaces 5.0)

时间:2014-06-01 15:23:47

标签: jsf file-upload jsf-2 primefaces

我尝试使用PrimeFaces上传文件,但上传完成后不会调用fileUploadListener方法。我添加了jar commons-io和commons-fileupload。

我正在使用

  • glassifish 3.1
  • primafaces 5.0
  • eclipseLink
  • jsf 2.

我搜索了很多但没有任何效果。这是我的代码。

托管Bean:

package image;
import java.io.IOException;
import java.io.InputStream;

import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.bean.ManagedBean;

import org.apache.commons.io.IOUtils;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;


@ManagedBean(name="uploadBean")
@SessionScoped
public class UploadBean {

private UploadedFile file;
private Fichier fichier;

@EJB
private FichierDao fichierDao;

public UploadBean()
{
    fichier = new Fichier();
}
public void handleFileUpload(FileUploadEvent event) {
   file= event.getFile();
  // System.out.print("gfghfhgf"+file.getSize());

}
// Store file in the database
public void storeImage() {

    System.out.println("11111mamlamlmalmalmlkljkjkhkjhjhjghj");
    try {
        InputStream is =file.getInputstream() ;
        fichier.setKey(IOUtils.toByteArray(is));
        System.out.println("22222222mamlamlmalmalmlkljkjkhkjhjhjghj");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        System.out.println("33333333333mamlamlmalmalmlkljkjkhkjhjhjghj");
    }
    System.out.println("44444444444mamlamlmalmalmlkljkjkhkjhjhjghj");

    fichierDao.create(fichier);
    System.out.println("55555555555mamlamlmalmalmlkljkjkhkjhjhjghj");
}

public UploadedFile getFile() {
    return file;
}

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

public Fichier getFichier() {
    return fichier;
}   
}

页面xhtml

   <!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta charset="utf-8" />
</h:head>
<h:body>
    <h:outputText value="PrimeFaces Single Upload"
        style="font:30px bold; margin-left:15%;" />
    <h:form enctype="multipart/form-data">
        <p:fileUpload
        fileUploadListener="#{uploadBean.handleFileUpload}" ajax="false"
            mode="advanced"
            label="Choisir une image"
            update="messages" 
            sizeLimit="100000" 
            fileLimit="1"
            invalidSizeMessage="La taille maximale du fichier est 1  Megabyte !"
            invalidFileMessage="Charger des images"
            allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
        <p:growl id="messages" showDetail="true" />

     <p:commandButton value="Submit"  action="#{uploadBean.storeImage}" />
    </h:form>

</h:body>
</html>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <!-- Changer cette valeur à "Production" lors du déploiement final de l'application -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>
    <context-param>
        <param-name>primefaces.UPLOADER</param-name>
        <param-value>commons</param-value>
    </context-param>
    <context-param>
        <param-name>
            javax.faces.INTERPRET_EMPTY_STRING_SUBMITTED_VALUES_AS_NULL
        </param-name>
        <param-value>true</param-value>
    </context-param>

    <filter>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <filter-class>org.primefaces.webapp.filter.FileUploadFilter</filter-class>
        <init-param>
            <param-name>thresholdSize</param-name>
            <param-value>10240</param-value> <!-- 10 Mb -->
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>PrimeFaces FileUpload Filter</filter-name>
        <servlet-name>Faces Servlet</servlet-name>
        <dispatcher>FORWARD</dispatcher>
    </filter-mapping>

    <!-- Déclaration du contrôleur central de JSF : la FacesServlet -->
    <servlet>
        <servlet-name>Faces Servlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- Mapping : association des requêtes dont le fichier porte l'extension 
        .xhtml à la FacesServlet -->
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>

    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>page1.xhtml</welcome-file>
    </welcome-file-list>
</web-app>`

2 个答案:

答案 0 :(得分:2)

在提交“文件上载”或“文件下载”表单的任何命令组件上使用ajax="false"

由于上传/下载是HTTP GET请求,因此不使用确实是POST请求的Ajax提交。

<p:commandButton value="Submit" ajax="false"  action="#{uploadBean.storeImage}" />

答案 1 :(得分:0)

以下是使用primefaces 5.0上传文件的示例;你不需要commons-fileupload-1.3.1.jar和commons-io-2.4.jar;而且你不需要更改web.xml,并在h:form中声明growl而不是在p:fileUpload中声明更多信息,请参阅How to upload files in primefaces

托管Bean:

package image;
    import java.io.IOException;
    import java.io.InputStream;

    import javax.ejb.EJB;
    import javax.enterprise.context.SessionScoped;
    import javax.faces.bean.ManagedBean;

    import org.apache.commons.io.IOUtils;
    import org.primefaces.model.UploadedFile;


    @ManagedBean(name="uploadBean")
    @SessionScoped
    public class UploadBean {

    private UploadedFile file;
    private Fichier fichier;

    @EJB
    private FichierDao fichierDao;

    public UploadBean()
    {
        fichier = new Fichier();
    }

    // Store file in the database
    public void storeImage() {

    if(!(file.getContentType().equalsIgnoreCase("image type"))){ 
        FacesMessage message = new FacesMessage("Not Uploaded","Required file types: ");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }
        if(!(file.getSize() <= 10000000)){ // size
        FacesMessage message = new FacesMessage("Not Uploaded","file size should be less than or equal  to 10mb");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }
        if(file.getSize()==0){
        FacesMessage message = new FacesMessage("","select a file");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }
        if((file.getSize() > 0 && file.getSize() <= 10000000) && (file.getContentType().equalsIgnoreCase("application/vnd.ms-excel")) ){

//your code here for storing image
 System.out.println("11111mamlamlmalmalmlkljkjkhkjhjhjghj");
        try {
            InputStream is =file.getInputstream() ;
            fichier.setKey(IOUtils.toByteArray(is));
            System.out.println("22222222mamlamlmalmalmlkljkjkhkjhjhjghj");
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("33333333333mamlamlmalmalmlkljkjkhkjhjhjghj");
        }
        System.out.println("44444444444mamlamlmalmalmlkljkjkhkjhjhjghj");

        fichierDao.create(fichier);
        System.out.println("55555555555mamlamlmalmalmlkljkjkhkjhjhjghj");

//your code above for storing image

       FacesMessage message = new FacesMessage("Succesfull", file.getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
        }

    }

    public UploadedFile getFile() {
        return file;
    }

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

    public Fichier getFichier() {
        return fichier;
    }   
    }

页面xhmtl:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <meta charset="utf-8" />
</h:head>
<h:body>
    <h:outputText value="PrimeFaces Single Upload"
        style="font:30px bold; margin-left:15%;" />
    <h:form enctype="multipart/form-data">
 <p:growl id="messages" showDetail="true" />
        <p:fileUpload
       value="#{uploadBean.file}" mode="simple"/>
     <p:commandButton value="Submit"  actionListener="#{uploadBean.storeImage}" 
ajax="false" disabled="false"/>
    </h:form>

</h:body>
</html>