Passing value to the backing bean with PrimeFaces file upload

北慕城南 提交于 2019-12-23 03:29:34

问题


I am trying to upload the file and pass one parameter from select box with PrimeFaces 3.5.

This is my form:

<h:form id="idAssessmentsUploadForm" enctype="multipart/form-data">

    <h:panelGrid cellspacing="10" styleClass="standard-panel" columns="2" id="idAssessmentsUploadPanelGrid">

        <h:outputText value="#{msg['application.assessmentsUploadRequest.loader']}"/>
        <p:selectOneMenu id="idLoader"
                         style="width: 230px;"
                         required="true"
                         value="#{configurationBean.loaderName}">

            <f:selectItems value="#{configurationBean.loaders}"/>

        </p:selectOneMenu>

    </h:panelGrid>

    <p:fileUpload fileUploadListener="#{configurationAction.processConfigurationUpload}"
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                  update="messages"
                  mode="advanced"/>

</h:form>

ConfiguratioBean is just a JSF @ViewScoped bean which contains getter and setter for loaderName.

My ConfigurationAction bean:

@ManagedBean(name = Beans.CONFIGURATION_ACTION)
@ViewScoped
public class ConfigurationAction extends BaseAction {

    public void processConfigurationUpload(FileUploadEvent event) {

        ConfigurationBean configurationBean = getBean(Beans.CONFIGURATION_BEAN);

        UploadedFile file = event.getFile();

        addInfoMessage("Upload Successful");
    }

}

I am receiving the file when I click upload, but the parameter loaderName is always null from the configurationBean. If I try to switch file upload to simple mode, put the file as a value in configurationBean and have a command button to upload the single file, then it is working. But I need upload to be advanced. So the question is how to pass the parameter to the backing bean if PrimeFaces file upload form is in advanced mode?


回答1:


Use remoteCommand for this. For e.g.:

<h:form id="idAssessmentsUploadForm" enctype="multipart/form-data">

    <h:panelGrid cellspacing="10" styleClass="standard-panel" columns="2" id="idAssessmentsUploadPanelGrid">

        <h:outputText value="#{msg['application.assessmentsUploadRequest.loader']}"/>
        <p:selectOneMenu id="idLoader"
                         style="width: 230px;"
                         value="#{configurationBean.loaderName}"
                         required="true">

            <f:selectItems value="#{configurationBean.loaders}"/>

        </p:selectOneMenu>

    </h:panelGrid>

    <p:fileUpload fileUploadListener="#{configurationAction.processConfigurationUpload}"
                  allowTypes="/(\.|\/)(gif|jpe?g|png)$/"
                  required="true"
                  onstart="loadProperty()"
                  update="messages"
                  mode="advanced"/>

    <p:remoteCommand name="loadProperty">
        <f:setPropertyActionListener for="idLoader"
                                     value="#{configurationBean.loaderName}"
                                     target="#{configurationBean.loaderName}"/>
    </p:remoteCommand>

</h:form>

Not tested but should work.



来源:https://stackoverflow.com/questions/16836294/passing-value-to-the-backing-bean-with-primefaces-file-upload

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