以编程方式访问JSF应用程序中的属性文件 [英] Accessing properties file in a JSF application programmatically

查看:112
本文介绍了以编程方式访问JSF应用程序中的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过代码访问在我的JSF应用程序中使用的i18n属性文件. (想法是要有一个页面将其键和值实际显示为表格.)

I am trying to access the i18n properties file I'm using in my JSF application in code. (The idea is to have a page that displays its keys and values as a table actually.)

该项目是一个maven项目,位于src/resources/localization文件夹中,并部署在WEB-INF \ classes \ localization \ 中的war文件中

The project is a maven project, and in the src/resources/localization folder, and deployed in the war file in WEB-INF\classes\localization\

java.util.Properties prop = new java.util.Properties();
String path = "localization/stat_codes.properties";
InputStream foo = prop.getClass().getResourceAsStream(path);

但是无论我将路径变量设置为/WEB-INF/classes/localization/stat_codes.properties、localization.stat_codes.properties"等,变量foo都为空.类似的问题是

But the variable foo turns out to be null whatever I set the path variable to, /WEB-INF/classes/localization/stat_codes.properties, "localization.stat_codes.properties" etc. A similar question is here, but there is no helpful answer there as well.

推荐答案

Class#getResourceAsStream()可以采用相对于您在此处用作起点的Class位置的相对路径.因此,例如,如果该类位于com.example包中,并且您请求路径foo/filename.properties,则它将实际上加载com/example/foo/filename.properties文件.但是,如果您使用/foo/filename.properties,那么它将实际上从类路径根目录加载foo/filename.properties.

The Class#getResourceAsStream() can take a path which is relative to the location of the Class which you're using there as starting point. So, for example, if the class is located in the com.example package and you request the path foo/filename.properties, then it will actually load the com/example/foo/filename.properties file. But if you use /foo/filename.properties, then it will actually load foo/filename.properties from the classpath root.

因此,您的代码

java.util.Properties prop = new java.util.Properties();
String path = "localization/stat_codes.properties";
InputStream foo = prop.getClass().getResourceAsStream(path);

实际上会 查找java/util/localization/stat_codes.properties文件.

但是在具有复杂的多个类加载器层次结构的应用程序中,一个类加载器不是另一个.加载了核心Java类的类加载器不一定具有有关Webapp /WEB-INF/classes中文件的知识.因此,在路径前添加/不一定是解决方案,它仍会返回null.

But in applications with a complex multiple classloader hierarchy, the one classloader isn't the other. The classloader which loaded the core Java classes does not necessarily have knowledge about files which are in the webapp's /WEB-INF/classes. So prefixing the path with / will not necessarily be the solution, it would still return null.

如果可以保证当前类和属性文件在同一类加载器中可见(因为它们位于类路径的同一子根目录中,例如/WEB-INF/classes),则确实应该使用

If you can guarantee that the current class is visible by the same classloader as the properties files (because they're in the same sub-root of the classpath, e.g. /WEB-INF/classes, then you should indeed use

String path = "/localization/stat_codes.properties";
InputStream foo = this.getClass().getResourceAsStream(path);

但是,如果在某个时候,属性文件将被外部化,因为在运行时更容易维护/编辑,因此您不需要在每次编辑文件时都重新构建/重新部署/重新启动Webapp,那么上述内容就可以了.代码行也可能会失败.外部位置只能由其他类加载器访问.规范的解决方案是改为使用线程的上下文类加载器作为起点,它可以访问类路径中的所有所有资源.

But if at some point, the properties files will be externalized because of more easy maintenance/editing during runtime so that you don't need to rebuild/redeploy/restart the webapp whenever you want to edit the files, then the above line of code will likely fail as well. The externalized location would be only accessible by a different classloader. The canonical solution is to use the thread's context classloader as starting point instead, it has access to all resources in the classpath.

String path = "localization/stat_codes.properties";
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream foo = loader.getResourceAsStream(path);

(请注意,该路径不能采用以/开头的路径,它始终相对于公共根目录)

(note that this one cannot take a path starting with /, it's always relative to the common root)

  • Where to place and how to read configuration resource files in servlet based application?
  • ExternalContext#getResourceAsStream() returns null, where to place the resource file?

这篇关于以编程方式访问JSF应用程序中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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