2010-07-14 83 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中的值,並建立各自的環境中有在建過程中的一步,這將替代需要從環境所要求的特性文件中的值。