【问题标题】:To set values from .properties file into web.xml将 .properties 文件中的值设置为 web.xml
【发布时间】:2012-10-12 00:00:26
【问题描述】:

我有一个 web.xml 文件,我想将属性文件中的多个值读入 web.xml。这可能吗? .如果是,如何?我可以在 java 类本身中访问这个属性文件吗?我专门使用 ant 来构建我的项目。对此的任何帮助表示赞赏。

【问题讨论】:

    标签: xml tomcat ant gradle properties-file


    【解决方案1】:

    您可以使用占位符来做到这一点。 例如(使用 Gradle):

    gradle.properties:

    myProp1=abc
    myProp2=def
    

    build.gradle:

    war {
        eachFile {
            if (it.name == 'web.xml') {
                it.filter {String line -> line.replaceAll('\\$\\{textToReplace1\\}', myProp1) }
                it.filter {String line -> line.replaceAll('\\$\\{textToReplace2\\}', myProp2) }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:
          properties.load(Connector.class.getResourceAsStream("path/to/properties/file"));
      
              this.DRIVER_CLASS = properties.getProperty("attribute/mentioned/in/the/property/file");
              this.DATABASE_URL = properties.getProperty("another/attribute/mentioned/in/the/property/file");
              this.databaseName = properties.getProperty("other/attribute/mentioned/in/the/property/file");
              this.username = properties.getProperty("other/attribute/mentioned/in/the/property");
      
          And so on...
      

      属性文件的格式与上面“Cengiz”显示的格式相同。 这里 this.variables 是我的私有变量,可以是任何东西。 此外,属性文件需要位于类的顶层层次结构中。

      这解决了我的疑问。 谢谢你的帮助。

      【讨论】:

      • 我通过不访问 web.xml 中的属性文件而是直接访问 java 文件来解决这个问题。采取了相反的方式。但最重要的是让代码正常工作。也这样做了。:)
      猜你喜欢
      • 2012-08-19
      • 2011-09-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-16
      • 2018-09-16
      • 2011-01-31
      • 2018-02-20
      • 1970-01-01
      相关资源
      最近更新 更多