Issue
When attempting to get an instance to a custom taglib within a controller, via the following:
def myTagLib = grailsApplication.mainContext.getBean('com.mypackage.MyTagLib')
I recieve the following error:
No bean named 'com.mypackage.MyTagLib' is defined. Stacktrace follows: org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'com.mypackage.MyTagLib' is defined
However, I am able to get an instance of a built in taglib without issue:
def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
According to the following, this should work. Does anyone have any ideas why my taglib would not be recognized as a valid bean?
I am using Grails 2.4.0
Solution
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)
Answered By - rmlan
Answer Checked By - Candace Johnson (JavaFixing Volunteer)