Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use grails tag outside of GSP?

For example, i can put

 <g:createLink controller="user" action="show" /> 

inside a .gsp file and it will work nicely.

But also I'd like to use the same closure createLink inside a .groovy file which is not part of the grails views

like image 649
Azder Avatar asked Jul 28 '09 03:07

Azder


People also ask

What is taglib in Grails?

grails taglib educational. Grails tag libraries are designed to provide content formatting capabilities right on a GSP page. The 'tag' is an action that can be declared in a form of an HTML element. This action can accept an enclosed content and parameters defined as the element attributes to render a modified HTML.

What is GSP in Java?

Groovy Servers Pages (or GSP for short) is Grails' view technology. It is designed to be familiar for users of technologies such as ASP and JSP, but to be far more flexible and intuitive.


4 Answers

You can use taglib methods from Grails controllers, for example:

def userShow = g.createLink(controller:"user", action:"show")

For builtin taglibs (or those in the g namespace) you can omit the namespace prefix in the method call.

like image 57
John Wagenleitner Avatar answered Oct 23 '22 21:10

John Wagenleitner


Inject the grailsApplication into your service/filter.

def grailsApplication

And get the Spring bean.

def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def userShow = g.createLink(controller: 'user', action: 'show')
like image 45
Matt Christianson Avatar answered Oct 23 '22 21:10

Matt Christianson


For unmanaged classes you can reference the g taglib with:

def g = ApplicationHolder.application.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
like image 33
James Allman Avatar answered Oct 23 '22 23:10

James Allman


The native way to do this as of Grails 2.0 outside of controllers (so for services, async jobs, etc) is to use the LinkGenerator class. Works everywhere and mentioned in the official docs. See example here

http://mrhaki.blogspot.ca/2012/01/grails-goodness-generate-links-outside.html

like image 6
Peter Avatar answered Oct 23 '22 22:10

Peter