How to set a bean property before uploading a file using rich:fileUpload?

白昼怎懂夜的黑 提交于 2020-01-06 07:13:40

问题


My application has a modal panel where the user can upload files and choose a "document type" in a drop-down select.

I was using an <f:setPropertyActionListener> to set the document type value during the upload event, but sometimes the property is set after the upload has been processed. Probably it's happening because another request is being generated, and this request is handled by another web container thread.

<rich:modalPanel id="attachFiles" autosized="true">
    <h:form id="formUpload" enctype="multipart/form-data">
        <h:selectOneMenu id="docType"  value="#{myMB.docType}" required="true" >
            <f:selectItems value="#{myMB.docTypesSelectItems}"  />
    </h:selectOneMenu>`
        <rich:fileUpload id="upload" fileUploadListener="#{myMB.handleUpload}">
            <a4j:support event="onupload">
                <f:setPropertyActionListener value="#{myMB.docType}" 
                    target="#{myMB.docType}" />
            </a4j:support>
        </rich:fileUpload>
</rich:modalPanel>

When it happens, the value of myMB.docTypeis null during the execution of myMB.handleUpload, which is not expected, since the field is supposed to be required.

Is there a way to assure that the method myMB.handleUpload is executed only after the property of docType has been set?


回答1:


<f:setPropertyActionListener value="#{myMB.docType}" target="#{myMB.docType}" />

I don't get you. The target is the same as the value. You're basically setting the target's value with self. Isn't the value itself simply already null?

Anyway, I don't do RichFaces, so I can't go in detail, but I know that it's using Flash under the covers for the upload component and that such a construction usually fires a separate (and standalone) request which doesn't take all other HTML form parameters into account. The "normal" JSF inputs comes thereafter in a separate HTTP request. So you're kind of lost here without bringing in some nasty JS/ajax hacks. At least, in theory.

Your best bet is to get hold of the uploaded file as a bean propery in the listener method and then process that further in the normal bean's action method (the one attached to some UICommand component in the same form).




回答2:


I had a similar issue.

Change

<a4j:support event="onupload">

to

<a4j:support event="onclick">

The set document type action will be executed before upload file. Exactly when the explorer file system is opened




回答3:


I would add Ajax capability to the select component instead. This way, the bean value is immediately updated each time the user changes the value of the select. Inside your file uploading method you can then rely on the bean value to represent the most recent selection the user has made.

You would only have to take care of the case, where the user starts the file upload without touching the select. Either you would need to have a sensible default value or you would have to take care of a non-selection and make the select field somehow required before uploading a file.



来源:https://stackoverflow.com/questions/4158505/how-to-set-a-bean-property-before-uploading-a-file-using-richfileupload

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