JSF / Prime(FileUpload组件)multipart-form不加载managed-bean类参数

问题描述 投票:1回答:3

我正在使用在glassfifh 3.1上运行的NB 7.2.1开发一个JSF / Primefaces 3.4网络应用程序。因此,在提交multipart-form enctype时,'FileUploadListener'触发的事件不允许加载另一个类属性(如其他JSF inputText中的名称或年龄)。为什么?

这是观点:

  <h:form enctype="multipart/form-data">
    <p:outputLabel value="Nome" for="nome" />
    <p:inputText value="#{controller.nome}" id="nome" />
    <br />
    <p:fileUpload mode="advanced" multiple="true" fileUploadListener="#{controller.doSubmit}" />
  </h:form>

这是豆子:

@ManagedBean
@SessionScoped
public class Controller {

  private String nome;

  public String getNome() {
    return nome;
  }
  public void setNome(String nome) {
    this.nome = nome;
  }

  public Controller() {
  }

  public void doSubmit(FileUploadEvent event) {
    System.out.println(getNome());
  }
}

在之前的'getName()'调用中,返回null。因此,一切都是相同的形式。为什么我可以检索event.getFile()并且不检索getNome()?

jsf primefaces
3个回答
4
投票

“上传”按钮不会提交整个表单。它只会上传文件。 要提交整个表单,您需要一个普通的提交按钮。

在文件上传监听器方法中,您只需将文件作为视图范围bean中的变量保存,以便您可以在与正常提交按钮关联的操作方法中对其执行必要的业务逻辑,以及所有其他输入值。

EG

<h:form enctype="multipart/form-data">
    <p:outputLabel value="Nome" for="nome" />
    <p:inputText value="#{controller.nome}" id="nome" />
    <br />
    <p:fileUpload mode="advanced" multiple="true" fileUploadListener="#{controller.handleUpload}" />
    <p:commandButton value="submit" action="#{controller.doSubmit}" />
</h:form>

@ManagedBean
@ViewScoped
public class Controller {

    private String nome;
    private List<File> files; // Whatever you need to get hold of all files.

    @PostConstruct
    public void init() {
        files = new ArrayList<File>();
    }

    public void handleUpload(FileUploadEvent event) {
        File file = save(event.getUploadedFile()); // Do your thing to save it.
        files.add(file);
    }

    public void doSubmit() {
        // Look, here you do the business job.
        System.out.println("Entered name: " + nome);
        System.out.println("Saved files: " + files);
    }

    // ...
}

1
投票

我能够通过使用PrimeFaces fileupload元素的process属性来解决类似的情况。我列出了我希望在文件上传后“提交”的所有组件,没有单独的按钮,只有三个。请注意,此解决方案在上载文件后按照列出的顺序“提交”进程中列出的组件值。同样,我在文件上传完成后使用update属性更新组件。在这种情况下,添加process =“name”,它应该做你需要的。

  <h:form enctype="multipart/form-data">
    <p:outputLabel value="Name" for="name" />
    <p:inputText value="#{controller.name}" id="name" />
    <br />
    <p:fileUpload process="name" mode="advanced" multiple="true" fileUploadListener="#{controller.handleUpload}" />
  </h:form>

您的托管bean将类似于:

    @ManagedBean
    @ViewScoped
    public class Controller {

        private String name;
        private List<File> files; // Whatever you need to get hold of all files.

        @PostConstruct
        public void init() {
            files = new ArrayList<File>();
        }

        public void handleUpload(FileUploadEvent event) {
            File file = save(event.getUploadedFile()); // Do your thing to save it.
            files.add(file);
        }

        public void setName(String name) {
            this.name = name;

            if(!files.isEmpty()) {
               // ... do something with name and files ...
            }
        }
   }

0
投票

我能够使用partialSubmit =“true”和process来解决类似的情况。 partial将部分处理的组件提交到ajax请求帖子。

<h:outputLabel for="saType" value="Sa Type"/>
                <p:selectOneMenu id="saType" required="true" value="#{saUploadController.saDetails.listType}">
                    <p:ajax event="change" partialSubmit="true"/>
                    <f:selectItem itemLabel="--select Type--"  itemValue=""/>
                    <f:selectItems value="#{saUploadController.saTypes}"/>
                </p:selectOneMenu>
                <h:outputLabel value="*select a file :"/> 
                <p:fileUpload id="uploadedFile1" value="#{saUploadController.uploadedFile}" mode="advanced" fileUploadListener="#{saUploadController.handleFileUpload}" allowTypes="/(\.|\/)(xml)$/" update="add:msgsAdd add:saType" process="saType"/>                      
                <p:message for="uploadedFile1" display="icon"/>

使用<p:ajax event="change" partialSubmit="true"/>我们可以部分提交下拉值,并通过process="saType"获取handleFileUpload方法的下拉项值

© www.soinside.com 2019 - 2024. All rights reserved.