How to send parameter to fileUploadListener in PrimeFaces fileUpload

≯℡__Kan透↙ 提交于 2019-12-04 03:30:59

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

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

user3667166

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

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.

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