我有一个grails应用程序,它从属性文件读取。我创建了一个管理页面,该页面允许用户查看此文件中的设置。我想允许用户更新各种属性,并使用Grails将其保存回属性文件。
我的问题是我似乎无法弄清楚如何将更改保存回属性文件。
这是我的代码,它从文件读取并更改属性:
def env = params.getAt("environment");
if (env != null){
//Read from Property file based on user selection
def props = new Properties()
def cl = Thread.currentThread().contextClassLoader
def filepath = env+'.properties'
props.load servletContext.getResourceAsStream("/WEB-INF/"+ filepath)
Enumeration e = props.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
def input = params.getAt(key)
if (input != null){
props.setProperty(key,input)
}
}
//props.store(propFile.newWriter(), null)
}
关于如何保存/覆盖/WEB-INF/example.properties中的example.properties文件的任何想法吗?
最佳答案
我认为您可能正在ServletContext中寻找getRealPath方法
http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)
一个简单的示例,向您展示如何使用它
http://82.157.70.109/mirrorbooks/javaservletjspcookbook/0596005725_jsvltjspckbk-chp-14-sect-7.html
关于spring - 将更改保存到Grails中的属性文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15821478/