来自fleUploadListener的验证消息未显示在`<p:dialog>`中

时间:2015-12-30 18:58:25

标签: primefaces jsf-2.2

我有<p:dialog>我正在上传文件。

<p:dialog id="uploadFileDialog"
        header="Upload File" widgetVar="uploadFileDialog" 
        modal="true" minHeight="40">
  <p:messages id="fileUploadMsgs" showDetail="true" autoUpdate="true"  
              closable="true" redisplay="false"/>
     <p:fileUpload id="uploadFile" mode="advanced"
                multiple="false" allowTypes="/(\.|\/)(docx|doc|pdf)$/"
                fileUploadListener="#{testMB.uploadFileListener}"
                auto="false" onstart="PF('ajaxStatusDialog').show()"
                oncomplete="PF('ajaxStatusDialog').hide()">    
        </p:fileUpload>
</p:dialog>

在上传监听器文件中,我正在查看数据库中是否已存在某个文件,如果是,我需要在对话框中抛出并返回错误。我尝试设置globalOnly="true"并在RequestContext.getCurrentInstance().addMessage中将客户端ID传递为null,但它不起作用。我甚至尝试在addMessage中传递<p:messages>的id,但这也不起作用。任何帮助都非常感谢。

编辑:我的fileUploadListener代码

    public void uploadFileListener(FileUploadEvent event){
            boolean success = false;
        List<Files> fileList = getDbFilesList();            
           if(fileList == null){
                success = handleDocumentUpload(event);
            }else{
                success = false;
                FacesContext.getCurrentInstance().addMessage(
                        "fileUploadMsgs",
                        new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                "Error : ",
                                "File Already Exists."));
            }
        }

我正在使用primefaces 5.2和jsf 2.2。谢谢!

1 个答案:

答案 0 :(得分:0)

我找到了让它发挥作用的方法。请参阅下面的更新代码。

<p:dialog id="uploadFileDialog"
        header="Upload File" widgetVar="uploadFileDialog" 
        modal="true" minHeight="40">
<h:form id="testForm>  
<p:messages id="fileUploadMsgs" showDetail="true" autoUpdate="true"  
              closable="true" redisplay="true"/>
     <p:fileUpload id="uploadFile" mode="advanced"
                multiple="false" allowTypes="/(\.|\/)(docx|doc|pdf)$/"
                fileUploadListener="#{testMB.uploadFileListener}"
                auto="false" onstart="PF('ajaxStatusDialog').show()"
                oncomplete="PF('ajaxStatusDialog').hide()" update="fileUploadMsgs">    
        </p:fileUpload>
</h:form>
</p:dialog>

文件上传监听器的代码如下:

public void uploadFileListener(FileUploadEvent event){
            boolean success = false;
        List<Files> fileList = getDbFilesList();            
           if(fileList == null){
                success = handleDocumentUpload(event);
            }else{
                success = false;
                FacesContext.getCurrentInstance().addMessage(
                        "testForm:uploadFile",
                        new FacesMessage(FacesMessage.SEVERITY_ERROR,
                                "Error : ",
                                "File Already Exists."));
            }
        }