Using Sling in a tag file
I've been doing some experimenting using tag libraries. I started out following a classic approach by implementing my tag library as a Java class. Everything works fine but understanding how everything fits together is somewhat complicated. I decided to give tag *files* a try, mainly because one of the reasons why some people appear to prefer them is that they are simpler to implement and understand (i.e. they are just a special type of JSP).
I am actually using JSP Documents (JSPX) and as a consequence tagx files, but thats mostly incidental.
Again I have everything working BUT, my tag file (and library) make a call to an OSGi service and to get a reference to the service instance I am using :-
sling.getService(myservice_interface.class)
I don't appear to be able to resolve a reference to sling in my tag file at least not by simply including a taglib directive in my file (or rather the tagx equivalent XML namespace declaration :-
<jsp:root xmlns="http://www.w3.org/1999/xhtml" xmlns:jsp="http://java.sun.com/JSP/Page" xmlns:sling="http://sling.apache.org/taglibs/sling/1.0" version="2.1">
If I subsequently try and use sling.getService(...) I just get a 'cannot resolve sling' exception.
I also tried using various import directives, but couldn't find one that had any [positive] effect. E.g. :-
<jsp:directive.tag import="org.apache.sling.xxx"/>
I *have* been able to get it to work by PASSING a reference to sling as an attribute FROM the calling JSP :-
JSP
<h1><aviva-aem:helloWorld name="AEM User" sling="${sling}"/></h1>mytagfile.tagx
<jsp:directive.attribute name="sling" rtexprvalue="true" required="true" type="org.apache.sling.api.scripting.SlingScriptHelper"/>
The call to sling.getService(...) now operately successfully, ... but it seems a bit klunky ... is there a neater approach ??
Thanks
Fraser.