2010-07-14 30 views
8

Tôi muốn biết liệu có khả năng đặt thuộc tính trong web.xml bằng cách sử dụng tệp thuộc tính hay không. Ví dụ web.xml:Cách đặt giá trị trong web.xml bằng cách sử dụng tệp thuộc tính

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

application.properties sẽ là:

# My computer's name
my.computer=eniac

Cảm ơn câu trả lời. Tomas

+0

Kiểm tra sau đây: - http://stackoverflow.com/questions/15380817/properties-file-as-init-param-in-web-xml - http://stackoverflow.com/questions/12099008/how-to-include-values-from-properties-file-into-web-xml – Chepech

Trả lời

1

bạn không thể đặt giá trị từ tệp Properties nhưng bạn có thể đặt tệp thuộc tính và đọc tệp lúc chạy.

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

sau đó đọc tệp thuộc tính khi chạy.

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()); 

hy vọng trợ giúp này cũng vậy.

0

Bạn không thể có giá trị được thay thế trong web.xml như thế.

Một tùy chọn tôi có thể đề xuất nếu có thể chỉ có một tệp web.xml với trình giữ chỗ cho các giá trị và trong khi xây dựng cho mỗi môi trường có một bước trong quá trình xây dựng sẽ thay thế các giá trị bắt buộc từ tệp thuộc tính bắt buộc của môi trường đó.

Các vấn đề liên quan