【问题标题】:Show / Hide another element based on primefaces p:fileUpload显示/隐藏基于 primefaces p:fileUpload 的另一个元素
【发布时间】:2019-05-12 03:10:14
【问题描述】:

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

这是我的简单表格

<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();
}

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

【问题讨论】:

  • 在handeUpload 调用之前showLabel 是否设置为false?如果是这样,正如 Microbob 所说,您无法更新未渲染的组件。尝试用 panel 或 panelGrid 包装 outputLabel,然后更新 panel 或 panelGrid 而不是 outputLabel 本身

标签: primefaces jsf-2


【解决方案1】:

您的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 属性,这不是必需的,但更简洁。

【讨论】:

  • 但是我不想更新表格,因为在我当前的项目中如果我更新表格,将会有另一个对数据库的请求...顺便说一句,这只能通过更新表格来完成?
  • 您可以将您的标签和文件上传包装在一个新元素中。就像 Blablabla 所说,这可以是一个面板或一个带有 jsf:id 的简单 div。
  • stackoverflow.com/questions/951593/… 中解释的“jsf:id”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
  • 1970-01-01
相关资源
最近更新 更多