grails 2.0 - correct use of serverURL for production?

后端 未结 2 1395
后悔当初
后悔当初 2021-02-04 02:27

Grails 2.0 changed with the way it uses grails.serverURL for development and test environments (as described in the manual). However, I\'ve had several problems with serverURL

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 03:23

    Good question! I was just looking for the correct way to get the actual serverURL without explicitly needing to configure it in Config.groovy (in a Grails 2.1 application).

    Since Grails 2.0, there is a super-useful class called LinkGenerator that you can use virtually anywhere, for example in a Service:

    import org.codehaus.groovy.grails.web.mapping.LinkGenerator
    
    Class MyService {
        LinkGenerator grailsLinkGenerator
    
        String serverUrl() {
            // Generate: http://localhost:8080/link-generator
            grailsLinkGenerator.serverBaseURL
        }
    }
    

    Thanks to Mr. Haki for blogging about this!

提交回复
热议问题