gpt4 book ai didi

properties - 在 Struts 2 中放置属性文件的位置?

转载 作者:行者123 更新时间:2023-12-04 10:26:36 26 4
gpt4 key购买 nike

我有一个属性文件放在 Java 的 web 项目的根目录中。我正在使用 Struts 2。
我的代码无法读取属性文件。我应该在哪里保存我的属性文件?

我已经检查了默认路径,它是我安装 Eclipse 的地方。但我希望该系统应该从项目文件夹本身读取文件。

最佳答案

通常你应该把属性文件放到 src文件夹,以便您的应用程序能够在您运行应用程序时读取属性文件,属性文件是从 src 复制的。文件夹到 classes文件夹。据您所知classes文件夹应该是项目输出文件夹,所以它会被用作 classpath文件夹和应用程序能够加载属性文件,如果它在 classpath 上.

从类路径获取属性的示例:

Properties prop = new Properties();

try {
//load properties from the class path
prop.load(this.getClass().getClassLoader().getResourceAsStream("myproperties.properties"));

//get the property
System.out.println(prop.getProperty("mykey"));

} catch (IOException ex) {
ex.printStackTrace();
throw ex;
}

但是,如果您知道文件系统上文件的路径,则可以加载属性,在这种情况下使用
prop.load(new FileInputStream("/path/to/myproperties.properties"));

如果您在谈论 struts.properties

The framework uses a number of properties that can be changed to fit your needs. To change any of these properties, specify the property key and value in an struts.properties file. The properties file can be locate anywhere on the classpath, but it is typically found under /WEB-INF/classes.



如果您正在寻找消息资源属性,它可以在 struts.properties 中进行配置。或 struts.xml后者提供。
<constant name="struts.custom.i18n.resources" value="path/to/resources/MessageResources"/>

该值是文件路径 src/path/to/resources/MessageResources.properties
如果您正在寻找配置应用程序的正确方法,请考虑选择使用 EasyConf .

关于properties - 在 Struts 2 中放置属性文件的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580274/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com