Passing value to the backing bean with PrimeFaces

2019-06-05 03:47发布

问题:

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.