【问题标题】:How to reference file resource in a JSF Application如何在 JSF 应用程序中引用文件资源
【发布时间】:2010-12-04 10:23:53
【问题描述】:

我想从 bean 中动态引用 XSD,这怎么可能?我已经将 XSD 添加到项目中,因此它位于 GlassFish 域中的某个位置。

【问题讨论】:

    标签: java web-applications jsf glassfish


    【解决方案1】:

    使用ExternalContext

    如果要在 bean 中加载资源,请通过 getResourcegetResourceAsStream 进行:

    InputStream stream = FacesContext.getCurrentInstance().getExternalContext()
        .getResourceAsStream("/foo.xsd");
    

    如果要返回资源的 URL,请使用 getRequestContextPath 获取相对于主机根目录的路径:

    ExternalContext ext = FacesContext.getCurrentInstance()
        .getExternalContext();
    String path = ext.getRequestContextPath();
    path += path.endsWith("/") ? "foo.xsd" : "/foo.xsd";
    String url = ext.encodeResourceURL(path);
    

    【讨论】:

    • 非常感谢!我将它作为 InputStream,之后我能够通过 BufferedReader-InputStreamReader 组合读取 XSD :)
    猜你喜欢
    • 2013-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    相关资源
    最近更新 更多