【问题标题】:rich:fileUpload more file informationrich:file上传更多文件信息
【发布时间】:2012-06-24 21:13:26
【问题描述】:

我想上传很多文件,为此我选择了rich:fileUpload 控件。

我的问题是我需要为每个文件添加更多信息,例如我希望在该文件的应用程序中出现的标题。我该怎么做,然后发送到 fileUploadListener 方法以使用 id?

【问题讨论】:

  • 您使用的是什么射频版本?

标签: jsf upload richfaces


【解决方案1】:

根据您的问题,RichFaces FileUpload demo 包含同时处理 1 个或多个文件的文件上传所需的所有信息。

如果您想添加更多数据(如h:inputText 值和其他值),那么您应该使用valueChangeListener 而不是value 标签属性传递它们,因为fileUploadListener 是在ajax 调用中发生的事件,因此您的 UIComponents 不会调用属性的设置器。

一些解释行为的代码:

<h:panelGrid cols="2">
    <h:outputText value="File Title:">
    <h:inputText value="#{fileBean.fileTitle}" immediate="false"
        valueChangeListener="#{fileBean.valueChangeFileTitle}" />
    <h:outputText value="File:">
    <rich:fileUpload
        fileUploadListener="#{bean.fileUpload}">
    </rich:fileUpload>
</h:panelGrid>

处理请求的 Bean

public class Bean {

    private String fileTitle;

    public Bean() {
    }

    //getters and setters...
    public String getFileTitle() {
        return this.fileTitle;
    }

    public void setFileTitle(String fileTitle) {
        System.out.println("Calling the setter");
        this.fileTitle = fileTitle;
    }

    public void valueChangeFileTitle(ValueChangeEvent e) {
        System.out.println("Calling the ValueChangeListener");
        fileTitle = (String)e.getNewValue();
}

    //this will be invoked by an ajax call
    //the setter of the view won't be invoked for fileTitle
    //instead, we should take its value using valueChangeListener
    public void fileUpload(UploadEvent ue) {
        MyFileManager mfm = MyFileManager.getFileManager();
        MyFile myFile = new MyFile();
        myFile.setTitle(this.fileTitle);
        myFile.setName(ue.getUploadItem().getFileName());
        myFile.setData(ue.getUploadItem().getData());
        mfm.createFile(myFile);
    }
}

另外,避免在您的代码中使用System.out.println 调用,我这样做是为了让您了解将调用什么方法,而不是使用像Log4j 这样的记录器。

【讨论】:

  • 您好!谢谢你的答复。我不确定我是否完全符合我的意思。我希望每个文件都有一个文件标题的输入文本,当我按下上传按钮时将此信息发送给处理程序。
  • 你不能。您必须为每个文件提供标题,您有两种方法:1) 为您的用户添加的每个文件动态添加文件输入文本,2) 一次询问一个文件的标题。我的例子是第二种方式。
猜你喜欢
  • 2012-07-08
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
相关资源
最近更新 更多