is possible access 开发者_JS百科the applicationContext in the config file?!
What I`m trying to do is get the full path(server path) for my app!
Pretty much I want is:
/usr/tomcat/myapp/web-inf
If @Ben's getRealPath() solution does not do the trick, try to piece together absolute path based on catalina base property:
def path = System.getProperty("catalina.base") + 'relative/path/to/app'
Maybe I misunderstand your question but I guess your are trying to retrieve the URL of your web application similar to this one: http://localhost:8080/myawesomeapp
. Config.groovy
has a built in option grails.serverURL but this doesn't give you the web application subcontext. The result would be http://localhost:8080
. I am not aware of a different option that would also give you the full web app context URL.
Alternatively, you can always use a method like this in your Grails controller that would give you the full web app URL:
def getContextUrl(HttpServletRequest httpServletRequest) {
String scheme = httpServletRequest.scheme
String serverName = httpServletRequest.serverName
int serverPort = httpServletRequest.serverPort
String contextPath = httpServletRequest.contextPath
def url = new StringBuilder()
url <<= '://'
url <<= serverName
if(serverPort && serverPort != 80) {
url <<= ':'
url <<= serverPort
}
url.toString()
}
精彩评论