0

我有一个从属性文件中读取的 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 文件的任何想法?

4

1 回答 1

1

我认为您可能正在寻找ServletContext http://docs.oracle.com/javaee/6/api/javax/servlet/ServletContext.html#getRealPath(java.lang.String)中的getRealPath方法

向您展示如何使用它的简单示例

http://82.157.70.109/mirrorbooks/javaservletjspcookbook/0596005725_jsvltjspckbk-chp-14-sect-7.html
于 2013-04-04T23:18:00.947 回答