If you are using Grails 2.x, you can use the LinkGenerator API. Here is an example, I am reusing a domain class that I tested earlier, so Ignore non-URL functions.
class Parent { String pName static hasMany = [children:Child] static constraints = { } static transients = ['grailsLinkGenerator'] static Map options() { def linkGen = ContextUtil.getLinkGenerator(); return ['url':linkGen.link(controller: 'test', action: 'index')] } }
Utility class with static method
@Singleton class ContextUtil implements ApplicationContextAware { private ApplicationContext context void setApplicationContext(ApplicationContext context) { this.context = context } static LinkGenerator getLinkGenerator() { getInstance().context.getBean("grailsLinkGenerator") } }
Bean Def for the new Bean utility
beans = { contextUtil(ContextUtil) { bean -> bean.factoryMethod = 'getInstance' } }
If you need a base URL, add absolute:true
to the link invocation.
source share