Grails get controller by name

13 Oct

These several days i’ve been searching on how to map url request to the controller, and finally found it. First i code it in java, but i face some problems and move it into groovy. Here is the code

ApplicationContext ctx = (ApplicationContext) org.codehaus.groovy.grails.web.context.ServletContextHolder.getServletContext().getAttribute(org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes.APPLICATION_CONTEXT);
def grailsApp = ctx.getBean("grailsApplication")
def controllerName = ctx.getBean("grailsUrlMappingsHolder").match(request.getServletPath()).getParameters().controller
def controllerInstance = grailsApp.getArtefactByLogicalPropertyName("Controller", ctrlName)

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.