Andrey Shilov
Andrey Shilov

Reputation: 66

Can't adaptTo my own Sling Model

I have problem with Sling Models in CQ 5.6.1 project

I did all as have been 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 cant get why it happens

My own Sling Model

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

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

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

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

Can anyone help me with that?

My build pluging config in models Bundle

<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 use 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 dont have any errors while trying to adaptTo

<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>

Upvotes: 3

Views: 4554

Answers (2)

Jordan Shurmer
Jordan Shurmer

Reputation: 1066

Your Sling Model will have to be declared as adaptable from a Resource rather than a ValueMap

@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 only designed to support Resource and SlingHttpServletRequest adapting.

Many Sling projects want to be able to create model objects - POJOs which are automatically mapped from Sling objects, typically resources, but also request objects

...

Adapt multiple objects - minimal required Resource and SlingHttpServletRequest

You're already passing in the resource object from the jsp so shouldn't have to change anything there..

Upvotes: 4

santiagozky
santiagozky

Reputation: 2539

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

Upvotes: 0

Related Questions