iAMintoAEM
iAMintoAEM

Reputation: 71

Getting error while using adaptTo method of Sling while using Sling Models

We are trying a sample sling model implementation in AEM 6.0.

The sling model class without the imports is like this :

@Model(adaptables = Resource.class)
public class Header {   

    @Inject
    private String link;
    @Inject
    private String text;

    public String getLink() {
    return link;
    }
    public String getText() {
    return text;
    }

}

The sling model is being called in the jsp using the following lines of code

<sling:adaptTo adaptable="${resource}" adaptTo="com.mysite.models.Header" var="model"/>
<h2>${model.link}</h2>
<h2>${model.text}</h2> 

However we are getting the following error :

No tag "adaptTo" defined in tag library imported with prefix "sling"

We have imported the taglib using following statement:

<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling" %>

Initially, the 2.2.0 version of Apache Sling JSP Tag Library bundle was present. We also tried uploading the 2.2.4 version, but it didn't help.

Can somebody please guide if anything else is required for adaptTo tag to be available?

Upvotes: 3

Views: 1268

Answers (1)

Greg S
Greg S

Reputation: 41

Stumbled across this while trying to help a coworker debug a similar issue. At first I was able to replicate this behavior (AEM 6.1) by copying the segment from the Doc page: <sling:adaptTo adaptable="${resource}" adaptTo="org.apache.sling.api.resource.ValueMap" var="myProps" />

What I found on my local was that our custom global.jsp file reference the old, pre-Granite version at /libs/foundation/global.jsp. Within that file I saw it had <%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %>. As a quick test I deleted the /1.0 at the end and refreshed the page and BAM, it worked.

Upvotes: 4

Related Questions