Referencing spring beans from a Grails tag
Published by peter August 16th, 2007 in grails, spring, textile.Today I had a short look at referencing Spring beans from Grails. Since Grails is build on top of Spring this was actually 100% simpler then expected. Grails controllers and taglibs are autowired by name on startup.
Just for the fun I create a simple tag which depends on a Spring bean for converting textile markup into HTML:
The tagfile looks like this:
The file (TexttileTagLig.groovy in my case) is placed in the taglib subfolder. Closures in classes placed here will be available as tags in the g namespace your gsp pages.
In the code above you can clearly see the power of grails, writing tags in this way is really easy and nice. The attrs attribute of the 'textile' closure contains all attributes passed to the tag, we will use the text attribute.
The interesting part is the 'textileParser' attribute. This will be automatically injected by Spring, so all we have to do is define it in the resources.xml file in the spring directory:
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xsi:schemaLocation="
-
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
-
-
<bean id="textileParser" class="com.plink.plextile.TextParser"/>
-
</beans>
And modify the 'show.gsp' template in the view directory to use the tag:
-
<g:textile text="${task.description}"/>
And this is the result:




















0 Responses to “Referencing spring beans from a Grails tag”
Please Wait
Leave a Reply