根据 primefaces 显示/隐藏另一个元素 p:fileUpload

Show / Hide another element based on primefaces p:fileUpload

我试图显示一个标签,其值为用户上传文件的文件名...

这是我的简单表格

<h:form>            
        <p:outputLabel value="#{indexBacking.fileName}" rendered="#{indexBacking.showLabel}" id="fileNameLabel"/>                                      
        <p:fileUpload id="uploadImage" mode="advanced"
                      dragDropSupport="false" required="true"
                      sizeLimit="5120000"
                      requiredMessage="Harap upload KTP anda"
                      allowTypes="/(\.|\/)(gif|jpeg|png|pdf)$/"                                                  
                      fileUploadListener="#{indexBacking.handleUpload}"
                      update="fileNameLabel"                               
                      auto="true"/>
</h:form>   

这是我的后盾

public void handleUpload(FileUploadEvent event) {
    showLabel = true;
    System.out.println("file uploaded");
    UploadedFile file = event.getFile();
    fileName = file.getFileName();
}

但是我每次上传文件,标签都没有显示...谁能给我解释一下为什么?谢谢

您的 update="fileNameLabel" 应该解决 h:form 因为它是必须呈现的标签的包装器。

编辑 14.12.2018

尝试这样的事情:

<h:form> 
   <div jsf:id="uploadImageWrapper">           
        <p:outputLabel value="#{indexBacking.fileName}" rendered="#{indexBacking.showLabel}" id="fileNameLabel" for="uploadImage" />                                      
        <p:fileUpload id="uploadImage" mode="advanced"
                      dragDropSupport="false" required="true"
                      sizeLimit="5120000"
                      requiredMessage="Harap upload KTP anda"
                      allowTypes="/(\.|\/)(gif|jpeg|png|pdf)$/"                                                  
                      fileUploadListener="#{indexBacking.handleUpload}"
                      update="uploadImageWrapper"                               
                      auto="true"/>
   </div>
</h:form>   

请注意,我还在标签中添加了 for 属性,这不是必需的,但更清晰。