简体   繁体   English

使用PrimeFaces文件上传将值传递给辅助bean

[英]Passing value to the backing bean with PrimeFaces file upload

I am trying to upload the file and pass one parameter from select box with PrimeFaces 3.5. 我正在尝试上传文件并从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 . ConfiguratioBean只是一个JSF @ViewScoped豆包含getter和setter的loaderName

My ConfigurationAction bean: 我的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 . 我在单击上载时收到该文件,但参数loaderNameconfigurationBean始终为null 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. 如果我尝试将文件上传切换到简单模式,将文件作为值放在configurationBean并有一个命令按钮来上传单个文件,然后它就可以了。 But I need upload to be advanced . 但我需要上传才能advanced So the question is how to pass the parameter to the backing bean if PrimeFaces file upload form is in advanced mode? 所以问题是如果PrimeFaces文件上传表格处于高级模式,如何将参数传递给支持bean?

Use remoteCommand for this. 为此使用remoteCommand For eg: 例如:

<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. 没有测试但应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM