【问题标题】:Grails 3, change contextPath dynamically (servletContext?)Grails 3,动态更改 contextPath(servletContext?)
【发布时间】:2016-06-18 00:15:48
【问题描述】:

我在动态更改 grails 3 Web 应用程序的 contextPath 时遇到问题。如果我在 groovy.gsp (server.contextPath = "/myApp") 中更改它,它就可以工作,但我需要使用不同的名称来部署它:

myApp_A.war         =>  IP:Port/myApp_A
myApp_B.war         =>  IP:Port/myApp_B

In 可以获取目录名称(例如:myApp_A.war 的“myApp_A”)并动态读取正确的属性文件(myApp_A.properties)。之后,我尝试将此实例的上下文路径动态设置为 IP:Port/myApp_A

我尝试像这样在引导程序中更改 grails“server.contextPath”:

Holders.config.'server.contextPath' = '/myApp_A'

但这并不完全有效。在这种情况下,我将被重定向到IP:Port/myApp_A,但随后我得到了 404。网络服务仍在侦听静态配置(“/myApp”)。

我想我必须早点更改它(在引导程序中为时已晚),或者直接在 servletContext 中更改它。将Holders.config.'server.contextPath' 更改为新路径后,servletContext 仍然包含旧的静态路径(servletContext.getContextPath() = 旧路径)。

在:

class Application extends GrailsAutoConfiguration implements EnvironmentAware { 
...

我可以在创建 servletContext 之前读取我的设置并注入它们。有什么想法可以在那里设置 contextPath 吗?

问候, 圣杯粉丝

【问题讨论】:

    标签: servlets grails grails-3.0 contextpath


    【解决方案1】:

    在 application.groovy 中,编写设置上下文路径的逻辑。

    您可以设置 server.contextPath = "path_a"。

    所以

    if ( myCondition == true ) {
        server.contextPath = "path_a"
    } else {
        server.contextPath = "path_b"
    }
    

    【讨论】:

      【解决方案2】:

      在 application.yml 添加到每个环境。然后,当您为每个环境构建 WAR 时,您的上下文也会发生变化。

      grails.serverURL: 'http://localhost:8080/myapp'
      server:
        port: 8080
        contextPath: '/myapp'
      

      【讨论】:

      • 好的,但是我必须为每个新租户创建一个新版本。 (每个不同的 contextName)
      • Grails 3 使用 Gradle 构建。 Gradle 和 Jenkins 的结合可以让您更轻松地构建。设置它需要一些工作。但它会使代码更简洁
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-08-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多