将属性文件放在 Struts 2 的什么位置? [英] Where to put a properties file in Struts 2?

查看:12
本文介绍了将属性文件放在 Struts 2 的什么位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a property file placed in the root of the web project in Java. I am using Struts 2. My code is unable to read properties file. Where should I keep my properties file?

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

I have checked default path , it is where my Eclipse in installed. But I want that system should read file from project folder itself.

推荐答案

属性文件一般会去:

  1. 在类路径上,例如,作为资源打开,或
  2. 在客户端无法访问的位置,例如,在 /WEB-INF

哪个更合适取决于您的需求.基于类路径的文件允许捆绑默认属性文件而无需配置.例如,Log4J 将在类路径的根目录中查找 log4j.properties 作为其默认配置文件.

Which is more appropriate depends on your needs. Classpath-based files allow bundled default property files without configuration. For example, Log4J will look for log4j.properties at the root of the classpath as its default configuration file.

然而,这有时会导致问题,具体取决于类加载顺序:有时系统会拾取杂散"配置文件.自己配置它可能仍然是可取的;我仍然倾向于类路径,但配置文件也通常在 WEB-INF 中找到.两种方法都可以,两种样式都可以使用 JNDI、init 参数、环境变量或系统变量(例如,-D)进行配置.

This can occasionally lead to issues, however, depending on class loading order: sometimes the system can pick up a "stray" config file. Configuring it yourself may still be preferable; I still tend towards the classpath, but config files are also commonly found in WEB-INF. Either method works, and both styles can be configured using JNDI, init params, environment variables, or system variables (e.g., -D).

这篇关于将属性文件放在 Struts 2 的什么位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆