get a resource from the servletContext or applicat

To get the ApplicationContext, have your service implement ApplicationContextAware and declare an 'applicationContext' field. Then from within your service:

 

def resource = applicationContext.getResource("foo")

Accessing the Servlet Context:

import org.codehaus.groovy.grails.commons.ApplicationHolder

def applicationContext = ApplicationHolder.getApplication().getParentContext()

def servletContext = ApplicationHolder.getApplication().getParentContext().getServletContext()

Alternate way of accessing the Servlet Context:

 

import org.codehaus.groovy.grails.web.context.ServletContextHolder as SCH

def servletContext = SCH.servletContext

你可能感兴趣的:(Web,servlet,grails,groovy)