读取JSF2.0中的属性文件,该文件也可以在战争中使用 [英] Reading properties file in JSF2.0 which can work in war also

查看:97
本文介绍了读取JSF2.0中的属性文件,该文件也可以在战争中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要使用Glassfishv3 Web服务器在JSF2.0中读取properties文件(位于我的Web应用程序的root directory中),我正在使用以下代码-

To read a properties file in JSF2.0 with Glassfishv3 webserver, which is located at root directory of my web application, I am using below code-

    ServletContext ctx = (ServletContext) FacesContext
            .getCurrentInstance().getExternalContext().getContext();
    String deploymentDirectoryPath = ctx.getRealPath("/");
    Properties prop = new Properties();
    prop.load(new FileInputStream(deploymentDirectoryPath
            + File.separator + "portal-config.properties"));

下面是Web门户的屏幕截图-

Below is the screenshot of web portal-

在运行门户网站时,由于Glassfish域中不存在该文件,因此出现FileNotFound错误.

While running the portal I am getting FileNotFound Error, since the file is not present in glassfish domain.

有什么方法可以读取在开发阶段和战争文件中都可以使用的属性文件吗?

Is there any way to read properties file which can work in both the situations, at development stage and in war file also?

推荐答案

永远不要使用java.io.File引用Web资源.它对它所处的上下文一无所知.也不要使用ServletContext#getRealPath(),因为当服务器配置为在内存中而不是磁盘上扩展WAR文件时,它可能会返回null,这在第三方中是无法控制的主机.

You should never use java.io.File to refer web resources. It knows nothing about the context it is sitting in. You should also never use ServletContext#getRealPath() as it may return null when the server is configured to expand WAR file in memory instead of on disk, which is beyond your control in 3rd party hosts.

只需使用

Just use ExternalContext#getResourceAsStream() to get the web resource directly in flavor of an InputStream. It takes a path relative to the webcontent root.

ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
properties.load(ec.getResourceAsStream("/portal-config.properties"));

另请参见:

  • getResourceAsStream()与FileInputStream
  • servletcontext.getRealPath("/")的含义以及何时应该使用它
  • 其中在基于servlet的应用程序中放置以及如何读取配置资源文件?
  • See also:

    • getResourceAsStream() vs FileInputStream
    • What does servletcontext.getRealPath("/") mean and when should I use it
    • Where to place and how to read configuration resource files in servlet based application?
    • 更新,它似乎根本不是网络资源.您应该将文件移动到"WebContent"文件夹中,如屏幕截图所示.或者,最好使用/WEB-INF文件夹,以便没有人可以通过URL访问它.

      Update it does not seem to be a web resource at all. You should move the file into the "WebContent" folder as shown in the screenshot. Or, better, the /WEB-INF folder so that nobody can access it by URL.

      properties.load(ec.getResourceAsStream("/WEB-INF/portal-config.properties"));
      

      另一种方法是将其放在类路径中,如屏幕快照所示,位于"Java源"文件夹中.您无需将其放在包装中,这是可选的.假设您没有将其放入包装中,则可以这样做:

      An alternative would be to put it in the classpath, the "Java source" folder as shown in the screenshot. You don't need to put it in a package, that's optional. Assuming that you didn't put it in a package, then do so:

      ClassLoader cl = Thread.currentThread().getContextClassLoader();
      properties.load(cl.getResourceAsStream("portal-config.properties"));
      

      (请注意,路径不能以斜杠开头!)

      这篇关于读取JSF2.0中的属性文件,该文件也可以在战争中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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