Primefaces FacesMessage不从FileUploadEvent显示

时间:2011-03-11 23:10:02

标签: primefaces

有没有人能够在FileUploadEvent中添加一个FacesMessage时出现?我无法这样做......

<p:dialog header="#{bundle['dreamUpload.HEADER']}"
    widgetVar="dreamUploadDlg" modal="true" styleClass="dialog dialog2"
    draggable="false" resizable="false" showEffect="fade"
    hideEffect="fade">
    <div class="dialog-top-reg"></div>
    <div class="dialog-middle-reg">
        <div class="close-button">
            <h:form>
                <p:commandButton onclick="dreamUploadDlg.hide()" />
            </h:form>
        </div>
        <h:form class="dialog-content dialog-content2">
            <h1 class="upload-dream">
                <h:outputText value="#{bundle['dreamUpload.HEADER']}" />
            </h1>
            <p class="upload-dream">
                <h:outputText value="#{bundle['dreamUpload.SUBHEADER']}" />
            </p>
            <div class="upload-dream-wrap">
                <fieldset>
                    <p:inputText />
                    <p:growl id="messages" showSummary="true" showDetail="true" />
                    <p:fileUpload fileUploadListener="#{dreamFileUpload.handleFileUpload}" auto="true"  
                            allowTypes="*.jpg;*.png;*.gif;" description="Images"/> 
                </fieldset>
                <h:outputText class="requirements"
                    value="#{bundle['dreamUpload.upload.requirements.TEXT']}" />
            </div>
        </h:form>
    </div>
    <div class="dialog-bottom-reg"></div>
</p:dialog>
@Named
@Scope("request")
public class DreamFileUpload extends BaseAction{
@Inject
DreamFileUploadBean dreamFileUploadBean;

public void handleFileUpload(FileUploadEvent event) { 
    String path="/users/cgray/fileupload";
    //new File(path + "/" + album.getId()).mkdirs();
    File result = new File(path + "/" + event.getFile().getFileName());

    try {
        FileOutputStream fileOutputStream = new FileOutputStream(result);

        byte[] buffer = new byte[512000]; //50kb

        int bulk;
        InputStream inputStream = event.getFile().getInputstream();
        while (true) {
            bulk = inputStream.read(buffer);
            if (bulk < 0) {
                break;
            }
            fileOutputStream.write(buffer, 0, bulk);
            fileOutputStream.flush();
        }

        fileOutputStream.close();
        inputStream.close();

        FacesMessage msg = new FacesMessage("Succesful",
                event.getFile().getFileName() + " is uploaded.");
        FacesContext.getCurrentInstance().addMessage(null, msg);


    } catch (IOException e) {
        e.printStackTrace();
        FacesMessage error = new FacesMessage("The files were not uploaded!");
        FacesContext.getCurrentInstance().addMessage(null, error);
    }
}

public void close(CloseEvent e){

}
}

1 个答案:

答案 0 :(得分:0)

这都是ajax,所以你需要将update属性添加到fileUpload标签,如
    <p:fileUpload fileUploadListener="#{dreamFileUpload.handleFileUpload}" auto="true" allowTypes="*.jpg;*.png;*.gif;" description="Images" update="messages"/>