如何将 .properties 文件中的值包含到 web.xml 中? [英] How to include values from .properties file into web.xml?

查看:39
本文介绍了如何将 .properties 文件中的值包含到 web.xml 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将 file.properties 中的一些值包含到 WEB-INF/web.xml 中,如下所示:

I need to include some values from a file.properties into the WEB-INF/web.xml something like this:

<param-name>uploadDirectory</param-name>
<param-value>myFile.properties['keyForTheValue']</param-value>

我目前正在处理这个:

  • JBoss
  • JEE5

推荐答案

您可以添加此类,将文件中的所有属性添加到 JVM.并将这个类添加到 web.xml

You can add this class, that add all properties from your file to JVM. And add this class like context-listener to web.xml

public class InitVariables implements ServletContextListener
{

   @Override
   public void contextDestroyed(final ServletContextEvent event)
   {
   }

   @Override
   public void contextInitialized(final ServletContextEvent event)
   {
      final String props = "/file.properties";
      final Properties propsFromFile = new Properties();
      try
      {
         propsFromFile.load(getClass().getResourceAsStream(props));
      }
      catch (final IOException e)
      {
          // can't get resource
      }
      for (String prop : propsFromFile.stringPropertyNames())
      {
         if (System.getProperty(prop) == null)
         {
             System.setProperty(prop, propsFromFile.getProperty(prop));
         }
      }
   }
}  

在 web.xml 中

in web.xml

   <listener>       
      <listener-class>
         com.company.InitVariables
      </listener-class>
   </listener>  

现在您可以使用

System.getProperty(...)

或在 web.xml

or in web.xml

<param-name>param-name</param-name>
<param-value>${param-name}</param-value>

这篇关于如何将 .properties 文件中的值包含到 web.xml 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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