Search code examples
grailsparameterstagsgsp

Relative URL to server path conversion - Grails


In a custom tag, I am receiving as a parameter the url of a file, which I need to open.

I have this

/content/data.html

which is the output from

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

and I need the 'server path':

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

Solution

  • You can use the Spring application context to find resources. This works if it's under web-app folder:

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