2011-07-21 33 views
2

在自定義標記中,我收到了一個文件的URL,我需要打開它。服務器路徑轉換的相對URL - Grails

我有這個

/content/data.html 

這是從

${createLinkTo(dir:'content',file:'data.html')}: 

輸出和我需要的 '服務器路徑':

C:\mygrailsapp\web-app\content\data.html 

回答

3

可以使用Spring應用程序上下文找到資源。這工作,如果它是在Web應用程序文件夾下:

class FooController { 

    def grailsApplication 

    def myAction = { 
     String path = params.path // '/content/data.html' 
     def resource = grailsApplication.mainContext.getResource(path) 
     String text = resource.inputStream.text 
     ... 
    } 
} 
+0

我喜歡這種方法比我的更好,所以我刪除了我的答案。 – Gregg