我在控制器中使用 request.getServerName() ,但我正在将我的功能移动到服务中。如何找到运行应用程序的主机名?
1 回答
If you know that the service method will only ever be called from a controller action then you can use the Spring RequestContextHolder
to get access to the current GrailsWebRequest
, and from there the request
object. But you might be better off passing in the request
from the controller or simply trusting grailsApplication.config.grails.serverURL
- ideally services shouldn't depend directly on web-tier classes.
If you need this info in a context where you can't use the injected grailsApplication
(a constructor, field initializer or static method/closure) then you can access the config via grails.util.Holders
.