9

我想知道是否有可能web.xml通过使用属性文件来设置属性。例如 web.xml:

<context-param>
  <param-name>Map.MyJNDI</param-name>
  <param-value>java:comp/env/jdbc/${my.computer}</param-value>
</context-param>

并且application.properties将是:

# My computer's name
my.computer=eniac
4

2 回答 2

2

您不能从Properties文件中设置值,但您可以设置属性文件并在运行时读取它。

<context-param>
    <param-name>propfile</param-name>
    <param-value>myproject.properties</param-value>
</context-param>

然后在运行时读取属性文件。

MyServlet myServlet = new MyServlet();

Properties  properties = new Properties();
//  get the properties file name 
String propfile = myServlet.getInitParameter("propfile");

// load the file 
properties.load(getClass().getClassLoader().getResourceAsStream(propfile));

// get the desire properties 
Object propName = properties.get("my.computer");
// out.println(propName.toString());

希望这对其他人也有帮助。

于 2012-06-17T07:48:29.660 回答
0

您不能像这样在 web.xml 中替换值。

如果可能的话,我可以建议一个选项,只要有一个带有占位符的模板 web.xml 用于值,并且在构建每个环境期间,在构建过程中都有一个步骤,它将从该环境的所需属性文件中替换所需的值。

于 2014-07-14T20:51:45.300 回答