【问题标题】:How to send parameter to fileUploadListener in PrimeFaces fileUpload如何在 PrimeFaces fileUpload 中向 fileUploadListener 发送参数
【发布时间】:2013-06-18 10:51:53
【问题描述】:

创建模型时,我想为模型保存图像。我正在使用 PrimeFaces 文件上传组件。当我保存图片时,我想知道特定图像所指的模型。这就是为什么我需要将模型的 id 发送到支持 bean。

是否有可能将模型的 id 发送到 fileUploadListener

<h:form enctype="multipart/form-data">
  <p:panelGrid columns="2">
    <h:outputLabel for="hotelName" value="#{msg.hotelName}"/>
    <p:inputText value="#{apartmentNew.name}" id="hotelName"/>
    <h:outputLabel for="hotelDescription" value="#{msg.hotelDescription}"/>
    <p:inputText value="#{apartmentNew.description}" id="hotelDescription"/>
    <h:outputLabel for="hotelImages" value="#{msg.hotelImages}"/>
    <h:form enctype="multipart/form-data">
      <p:fileUpload id="hotelImages"
                    fileUploadListener="#{apartments.handleImageUpload}"
                    mode="advanced"
                    sizeLimit="10000000"
                    allowTypes="/(\.|\/)(gif|jpe?g|png)$/">
      </p:fileUpload>
    </h:form>
  </p:panelGrid>
  <p:commandButton id="saveApartmentButton" value="#{msg.save}" action="save"/>
  <p:commandButton id="cancelCreationApartmentButton" value="#{msg.cancel}" 
     action="cancel"/>
</h:form>

【问题讨论】:

标签: file-upload jsf-2 primefaces spring-webflow


【解决方案1】:

不是通过请求参数。您可以通过组件属性来实现。

例如

<p:fileUpload ...>
    <f:attribute name="foo" value="bar" />
</p:fileUpload>

String foo = (String) event.getComponent().getAttributes().get("foo"); // bar

【讨论】:

  • 您必须将event.getComponent().getAttributes().get("foo"); 转换为字符串,否则将无法编译。所以应该是:String foo = (String) event.getComponent().getAttributes().get("foo"); // bar
【解决方案2】:

我需要将一个关键参数与上传的文件一起传递。我发现fileUploadListener 在 APPLY_REQUEST_VALUES 阶段执行,所以我不能在f:attribute 标记中使用 EL 表达式。我还尝试使用event.getComponent().findComponent("id") 来查找值,但尽管该组件存在,但该值是空的。我认为@ViewScoped bean 可以修复缺失值,但我顽固地试图将我的 bean 保持在@RequestScoped,直到我完全没有其他选择。最终,我不得不使用从http://forum.primefaces.org/viewtopic.php?f=3&t=6432 获得的FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")

【讨论】:

  • (String) event.getComponent().getAttributes().get() 使用名称,而不是 id
【解决方案3】:

类型错误:

String foo = event.getComponent().getAttributes().get("foo");

相反,这样做:

Object foo = event.getComponent().getAttributes().get("foo");

Integer foo = (Integer) event.getComponent().getAttributes().get("foo");

【讨论】:

  • 是的,您必须将其转换为正确的类型,无论是字符串、整数等
【解决方案4】:

你可以使用:

<div onclick="#{myBean.myMethod(myParam)}" >
                <p:fileUpload id="fileUpload" fileUploadListener="#{myBean.onFileUplod}" mode="advanced" dragDropSupport="true"
                    update=":messages" process="@this" >
                </p:fileUpload>
        </div>  

myBean 中的方法:

public void myMethod(String myParam) {

            selectedMyParam = myParam;
           }

然后你可以在 onFileUpload 方法中使用 selectedMyParam。

【讨论】:

  • 你测试过这个吗? onclick 用于调用 javascript,而不是 java。
  • 是的,我在我的应用程序中使用了这个解决方案并且效果很好。
  • 我 101% 确定 &lt;div onclick="#{myBean.myMethod(myParam)}" &gt; 在您单击时不会调用此方法。该方法可能(将!)在呈现页面并且输出为例如时被调用。单击完成时将调用一个引用 javascript 函数 that 的字符串。
  • 它很奇怪,因为它在我的应用程序中工作。我使用 java 和 primefaces/jsf xhtml 站点。单击
  • 你能用足够的 xhtml 和一个仍然证明它对你有用的 bean 来制作一个真正的小 minimal reproducible example 吗?我真的很想研究为什么它实际上可以工作,或者至少在功能上可以满足您的需求。
猜你喜欢
  • 2011-10-28
  • 2014-10-01
  • 2014-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2023-03-31
相关资源
最近更新 更多