代码之家  ›  专栏  ›  技术社区  ›  Bozho

获取config.groovy中的servletcontext(或如何获取当前上下文的实际路径)

  •  1
  • Bozho  · 技术社区  · 15 年前

    问题在标题中-如何获得 ServletContext 在里面 Config.groovy . 目的是获取当前上下文的实际(绝对)路径。

    2 回复  |  直到 15 年前
        1
  •  2
  •   Bozho    15 年前

    ServletContext

    def path = getClass().getProtectionDomain().getCodeSource().getLocation()
        .getFile().replace(getClass().getSimpleName() + ".class", "").substring(1);
    

    substring(1)

        2
  •  2
  •   Chris Frederick    15 年前

    Config.groovy

    def path = getClass().getProtectionDomain().getCodeSource().getLocation().getFile().replace("/WEB-INF/classes/" + getClass().getSimpleName() + ".class", "").substring(1);
    path = path.substring(path.lastIndexOf("/") + 1)
    
    println "path: $path ${path}"
    
    def env = System.getenv()
    if (!env['ISP_CONFIG']) {
        System.err.println 'Environment variable EXTERNAL_CONFIG_DIR is not set.'
    } else {
        grails.config.locations = [
            "file:${env['EXTERNAL_CONFIG_DIR']}/grails/${path}/grails-config.groovy",
            "file:${env['EXTERNAL_CONFIG_DIR']}/grails/${path}/DataSource.groovy"
        ]
    }
    
    推荐文章