0

当尝试通过以下方式将实例获取到控制器中的自定义标记库时:

def myTagLib = grailsApplication.mainContext.getBean('com.mypackage.MyTagLib')

我收到以下错误:

没有定义名为“com.mypackage.MyTagLib”的 bean。Stacktrace 如下: org.springframework.beans.factory.NoSuchBeanDefinitionException:没有定义名为 'com.mypackage.MyTagLib' 的 bean

但是,我能够毫无问题地获得内置 taglib 的实例:

def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')

根据以下内容,这应该有效。有谁知道为什么我的 taglib 不会被识别为有效的 bean?

我正在使用 Grails 2.4.0

4

1 回答 1

1

According to Section 8.3.5 of the Grails Documentation, if your taglib is namespaced (which is generally a good practice), you can simply call taglib methods by using your namespace as the prefix to the method call.

For instance, if you have a taglib namespaced "my" and it has a method called "smiley" that accepts an attribute "happy", you could have the following line in a controller with no extra imports necessary:

my.smiley(happy: true)
于 2014-08-28T14:28:48.337 回答