【问题标题】:Get file url in JSF 2.2在 JSF 2.2 中获取文件 url
【发布时间】:2014-11-19 09:08:39
【问题描述】:

我正在尝试在支持 bean 中创建一个 openfiledialog,以便我可以获取本地文件的 url。但是,标准 JSF 库似乎不支持打开/保存文件对话框。是否有另一种方法可以获取类似于 openfiledialog 的文件的 url?

这是必须发生的事情: 点击一个按钮 --> 向后台 bean 发送操作 --> show openfiledialog --> 获取文件的 url --> 将文件的 url 添加到字符串列表中。

我不确定如何完成上述粗体步骤。有没有人可以给我任何线索或帮助来完成这个?

(我正在使用 JSF 和 Primefaces,如果可能,我不希望使用其他外部库。)

【问题讨论】:

    标签: jsf-2 primefaces openfiledialog


    【解决方案1】:

    既然您使用 JSF 2.2 和 Primefaces,为什么不使用 primefaces 组件本身呢?

    下面是基本文件上传的Primefaces 5.0 showcase example

    <h:form enctype="multipart/form-data">
        <p:growl id="messages" showDetail="true" />
    
        <p:fileUpload value="#{fileUploadView.file}" mode="simple" skinSimple="true"/>
    
        <p:commandButton value="Submit" ajax="false" actionListener="#{fileUploadView.upload}" disabled="true" />
    </h:form>
    

    托管 Bean

    import org.primefaces.event.FileUploadEvent;
    import org.primefaces.model.UploadedFile;
    
    @ManagedBean
    public class FileUploadView {
    
        private UploadedFile file;
    
        public UploadedFile getFile() {
            return file;
        }
    
        public void setFile(UploadedFile file) {
            this.file = file;
        }
    
        public void upload() {
            if(file != null) {
                FacesMessage message = new FacesMessage("Succesful", file.getFileName() + " is uploaded.");
                FacesContext.getCurrentInstance().addMessage(null, message);
            }
        }
    }
    

    表单的enctype 属性是必需的。此外,为了使用 Primefaces 文件上传,您需要设置某些上下文参数和过滤器。您也可以参考相应的documentation

    【讨论】:

    • 不完全是我所说的,但我明白了!谢谢
    【解决方案2】:

    您可以获取上传到路径或数据库的文件的 url,不确定您是否在 google 中进行搜索,但我发现这些 URL 可能会对您有所帮助:

    http://balusc.blogspot.com/2007/07/fileservlet.html

    Download a file with JSF?

    您只需要打开一个包含文件链接的对话框,或者文件 url 可以存储在变量中以添加到列表中或您想要的任何内容。

    希望这会对你有所帮助。

    【讨论】:

    • 这两个链接都将提供有关如何下载文件的信息,该文件的位置是已知的。我想要完成的是文件搜索并将文件的 url/位置存储到一个变量中(因此该位置现在在正手上是已知的)
    猜你喜欢
    • 2014-02-13
    • 2013-09-12
    • 2018-06-12
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多