【问题标题】:Cannot read properties file with web app (Maven web app with JSF)无法使用 Web 应用程序读取属性文件(带有 JSF 的 Maven Web 应用程序)
【发布时间】:2013-12-22 11:20:11
【问题描述】:

我有一个属性文件:默认包内的 hibernate.properties。我正在尝试从另一个包中的类中读取它:com.somepackage.SomeClass。

在普通桌面应用程序中,以下输入流正在工作:

InputStream is = getClass().getResourceAsStream("/hibernate.properties");

但是对于我的网络应用程序(使用 Netbeans 创建的一个 Maven 网络应用程序,以 JSF 2.2 作为依赖项)

我尝试了以下替代方法:

使用类:

getClass().getResourceAsStream("/hibernate.properties");

使用外部上下文:

FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/hibernate.properties");

使用上下文类加载器:

Thread.currentThread().getContextClassLoader().getResourceAsStream("/hibernate.properties");

每个都返回 null。如何读取默认包内的属性文件?

【问题讨论】:

    标签: jsf web-applications properties-file


    【解决方案1】:

    我假设您正在查找文件的错误位置。 hibernate.properties 通常不会保存在部署的根目录中。打开您的 .war 文件并查找 hibernate.properties 的位置。如果您将应用程序部署到localhost:8080/app1,则需要调用/app1/ 来访问部署的内容。

    【讨论】:

    • 好的。奇怪的是,当我从 Netbeans 向导创建 hibernate.cfg.xml 时,它出现在里面:“其他来源(src/main/resources)”。我以为它会是 insde: 。让我把所有的资源放到 src/main/resources
    • 只需使用例如 winrar 打开您的 .war,然后查看它的真实位置。
    • 我自己创建了 hibernate.properties 文件。
    • 我的观点是你需要看看你的 .war 是如何创建的。 .war 中的位置很重要
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2016-05-23
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多