For example, i can put
例如,我可以把
<g:createLink controller="user" action="show" />
inside a .gsp file and it will work nicely.
在.gsp文件中,它会很好地工作。
But also I'd like to use the same closure createLink
inside a .groovy file which is not part of the grails views
但是我也想在.groovy文件中使用相同的闭包createLink,该文件不是grails视图的一部分
4 个解决方案
#1
You can use taglib methods from Grails controllers, for example:
您可以使用Grails控制器中的taglib方法,例如:
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.
对于内置标记库(或g命名空间中的标记库),您可以在方法调用中省略名称空间前缀。
#2
Inject the grailsApplication into your service/filter.
将grailsApplication注入您的服务/过滤器。
def grailsApplication
And get the Spring bean.
并获得Spring bean。
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def userShow = g.createLink(controller: 'user', action: 'show')
#3
For unmanaged classes you can reference the g
taglib with:
对于非托管类,您可以使用以下命令引用g taglib:
def g = ApplicationHolder.application.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
#4
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
从控制器之外的Grails 2.0(对于服务,异步作业等)执行此操作的本机方法是使用LinkGenerator类。到处工作,并在官方文档中提到。见这里的例子
http://mrhaki.blogspot.ca/2012/01/grails-goodness-generate-links-outside.html
#1
You can use taglib methods from Grails controllers, for example:
您可以使用Grails控制器中的taglib方法,例如:
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.
对于内置标记库(或g命名空间中的标记库),您可以在方法调用中省略名称空间前缀。
#2
Inject the grailsApplication into your service/filter.
将grailsApplication注入您的服务/过滤器。
def grailsApplication
And get the Spring bean.
并获得Spring bean。
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def userShow = g.createLink(controller: 'user', action: 'show')
#3
For unmanaged classes you can reference the g
taglib with:
对于非托管类,您可以使用以下命令引用g taglib:
def g = ApplicationHolder.application.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
#4
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
从控制器之外的Grails 2.0(对于服务,异步作业等)执行此操作的本机方法是使用LinkGenerator类。到处工作,并在官方文档中提到。见这里的例子
http://mrhaki.blogspot.ca/2012/01/grails-goodness-generate-links-outside.html