Can't Adapt To My Own Sling Model

I have a problem with Sling Models in a CQ 5.6.1 project

I did everything as it was written here -> http://www.wemblog.com/2014/11/how-to-use-sling-models-in-cq56.html?showComment=1417594209746#c3879427154987489876

But when I try to use

<sling:adaptTo adaptable="${resource}" adaptTo="com.my.client.core.models.MyModel" var="model"/>

      

but it returns null and I can't figure out why this is happening

My own model Sling

@Model(adaptables = ValueMap.class)
public interface MyModel {

    @Inject @Named(value = "jcr:title")
    public String getTitle();
}

      

I'm sure I can get "jcr: title" from the resource because I get it from adapting to ValueMap.class

<% String title = resource.adaptTo(ValueMap.class).get("jcr:title", String.class);%>
    <%=title%>

      

Can anyone help me with this?

My build pluging config in Bundle models

<build>
        <plugins>
            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-bundle-plugin</artifactId>
                <extensions>true</extensions>
                <configuration>
                    <instructions>
                        <Export-Package>
                            com.my.client.*
                        </Export-Package>
                        <Bundle-Category>models/Bundle-Category>
                        <Require-Bundle>org.apache.sling.models.api</Require-Bundle>
                        <Import-Package>
                            org.apache.log ;resolution:=optional,
                            org.apache.avalon.framework.logger ;resolution:=optional,
                            *
                        </Import-Package>
                        <Sling-Model-Packages>
                            com.my.client.core.models
                        </Sling-Model-Packages>
                    </instructions>
                </configuration>
            </plugin>
        </plugins>
    </build>

      

I am using both dependencies

 <dependency>
        <groupId>org.apache.sling</groupId>
        <artifactId>org.apache.sling.models.api</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.sling</groupId>
        <artifactId>org.apache.sling.models.impl</artifactId>
    </dependency>

      

and use in my view module, so I have no errors when trying to adapt to

<dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.scripting.jsp</artifactId>
            <version>2.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.scripting.jsp.taglib</artifactId>
            <version>2.2.0</version>
        </dependency>

      

+3


source to share


2 answers


Your Sling model should be declared as adaptable from Resource

, notValueMap

@Model(adaptables = Resource.class)
public interface MyModel {

    @Inject @Named(value = "jcr:title")
    public String getTitle();
}

      

You can see in the Sling Models docs that it is for support Resource

and SlingHttpServletRequest

adaptation only.



Many Sling projects want to be able to create model objects - POJOs that automatically map to Sling objects, usually resources, but also request objects

...

Adapt Multiple Objects - Minimum Resource Required and SlingHttpServletRequest

You are already passing in an object Resource

from jsp so no need to change anything there.

+4


source


you should either adapt from Resource.class or SlingHttpServletRequest.class, not ValueMap.class. in any case, the sling factory model can inject values ​​from the resource.



0


source







All Articles