How to send parameter to fileUploadListener in PrimeFaces fileUpload

徘徊边缘 提交于 2019-12-09 15:41:51

问题


When I create a model I would like to save images for a model. I am using PrimeFaces fileUpload component. When I save pictures I want to know to which model particular image refers to. That's why I need to send id of a model to backing bean.

Is there any possibility to send id of model to 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>

回答1:


Not via request parameters. You can do so via component attributes.

E.g.

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

with

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



回答2:


I needed to pass a key parameter along with the uploaded file. I found that fileUploadListener executes during the APPLY_REQUEST_VALUES phase, so I could not use an EL expression in the f:attribute tag. I also tried to find the value using event.getComponent().findComponent("id"), but although the component was present, the value was null. I think a @ViewScoped bean would fix the missing value, but I am stubbornly attempting to keep my beans at @RequestScoped until I have absolutely no other option. Ultimately, I had to use FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id") which I got from http://forum.primefaces.org/viewtopic.php?f=3&t=6432




回答3:


Error in types:

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

Instead, do it this way:

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

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



回答4:


You can use:

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

Method in myBean:

public void myMethod(String myParam) {

            selectedMyParam = myParam;
           }

Then you can use selectedMyParam in onFileUpload method.



来源:https://stackoverflow.com/questions/17233001/how-to-send-parameter-to-fileuploadlistener-in-primefaces-fileupload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!