Primefaces文件上传无效

时间:2017-07-26 09:09:42

标签: jsf file-upload primefaces

试图让jsf fileUploadListener工作只是似乎没有做任何尝试只是在点击{fileUploadView.handleFileUpload}时得到一个打印线似乎没有发生任何事情。 UI在localhost上加载正常:8080 / index.jsf点击选择文件后我选择了一个文件然后按下上传按钮没有任何反应日志或打印行没有错误从显示的角度打印所有内容似乎都没问题。

的index.xhtml

    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
          xmlns:f="http://xmlns.jcp.org/jsf/core"
          xmlns:p="http://primefaces.org/ui">

        <link type="text/css" rel="stylesheet" href="/project/faces/javax.faces.resource/theme.css?ln=primefaces-glass-x" />

        <f:view>
            <h:head>
                <meta charset="utf-8" />

                <title>test</title>
            </h:head>


            <h:body>
                <h:form>
                    <p:fileUpload fileUploadListener="#{fileUploadView.handleFileUpload}" mode="advanced" dragDropSupport="true"
                                  update="messages" sizeLimit="100000" allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />

                    <p:growl id="messages" showDetail="true" />
                </h:form>
            </h:body>
        </f:view>
    </html>

FileUploadView.java(bean)

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;

@ManagedBean
@ViewScoped
public class FileUploadView {

    private UploadedFile uploadedFile;

    public void handleFileUpload(FileUploadEvent event) {
        System.out.println("handel file upoad =-=-=-=-=-");
        FacesMessage message = new FacesMessage("Succesful", event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, message);
    }

    public UploadedFile getUploadedFile() {
        System.out.println("uploading file get =-=-=-=");
        return uploadedFile;
    }

    public void setUploadedFile(UploadedFile uploadedFile) {
                System.out.println("uploading file set =-=-=-=");

        this.uploadedFile = uploadedFile;
    }


}

2 个答案:

答案 0 :(得分:0)

在表单中添加enctype。


    enctype="multipart/form-data" in your <h:form>


    replace you  <h:form> with  <h:form enctype="multipart/form-data">

答案 1 :(得分:0)

他们在你的代码中看到了两个问题 第一个是你应该使用

<h:form enctype="multipart/form-data">
    your code here
</h:form>

并将ajax = false属性添加到commandButton

<h:form enctype="multipart/form-data">
    <p:growl id="messages" showDetail="true" />


        <p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>

        <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" disabled="true" />
    </h:form>

官方文件https://www.primefaces.org/showcase/ui/file/upload/basic.xhtml

无关Ijust注意到有趣的事情,上传不适用于primefaces官方展示xP