2010-07-14 87 views
8

我想知道是否有可能通过使用属性文件在web.xml中设置属性。例如在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

感谢您的回答。 Tomas

+0

检查这些: - http://stackoverflow.com/questions/15380817/properties-file-as-init-param-in-web-xml - http://stackoverflow.com/questions/12099008 /如何对包括值从 - 属性文件 - 到 - 网络的XML – Chepech 2013-11-01 17:34:16

回答

1

您无法从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()); 

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

0

你不能有这样的web.xml中取代值。

一种选择,我可以建议如果可能的话就必须有占位符的模板web.xml中的值,并建立各自的环境中有在建过程中的一步,这将替代需要从环境所要求的特性文件中的值。