12

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>
Sergii Shevchyk
  • 38,716
  • 12
  • 50
  • 61
  • You can find the solution from here : http://stackoverflow.com/questions/15911527/jsf-2-uploadedfile-is-null-in-managedbean-using-tomahawk ! – biqarboy Oct 07 '13 at 05:08

4 Answers4

43

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
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • You have to cast the `event.getComponent().getAttributes().get("foo");` as a String or this won't compile. So it would be: `String foo = (String) event.getComponent().getAttributes().get("foo"); // bar` – Steve Waters May 03 '16 at 12:15
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

Jeff E
  • 658
  • 1
  • 8
  • 19
0

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");
0

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.

4Money
  • 179
  • 1
  • 1
  • 10
  • Did you test this? onclick is for calling javascript, not java. – Kukeltje Jun 04 '18 at 18:12
  • Yes, i use this solution in my application and it worked fine. – 4Money Jun 06 '18 at 14:31
  • I'm 101% sure `
    ` does not call this method when you click. The method might (will!) be called when the page is rendered and if the output is e.g. a string that refers to a javascript function **that** will be called when the click is done.
    – Kukeltje Jun 06 '18 at 15:37
  • Its strange, because its working in my app. I use java and primefaces/jsf xhtml site. When – 4Money Jun 06 '18 at 15:59
  • Can you make a real small [mcve] with just enough xhtml and a bean that still demonstrates that it works for you? I'm really curious to investigate on why it actually works or at least functionally does what you need to have. – Kukeltje Jun 06 '18 at 16:59