Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
BedrockMission!

Learn More

View all

Sign in to view all badges

How to using Sling Models in Component Java Class

Avatar

Avatar
Validate 1
Level 2
pdg2491
Level 2

Likes

5 likes

Total Posts

6 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Applaud 5
View profile

Avatar
Validate 1
Level 2
pdg2491
Level 2

Likes

5 likes

Total Posts

6 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Applaud 5
View profile
pdg2491
Level 2

22-08-2019

Hi,

I want to use Sling Models to configure the component as I saw in the "Sling Models Vs WCMUsePojo'' posting.

through the tutorial and configured a custom component using Sling Models,

but Annotation fails when trying to place Java Class File in jcr_root / apps path.

Search for Use Case confirmed that WCMUsePojo is available.

How can i use Sling Models to local java-class in Component Folder?

( * pom.xml file went the same way as the tutorial setup. :  5 - Custom Component )

* Component Structure

└─apps

    └─wcde

        └─components

            └─content

                └─test

                    └─mid_hero

                        │  .content.xml

                        │  Controller.java

                        │  mid-hero.html

                        │  _cq_editConfig.xml

                        │

                        └─_cq_dialog

                                .content.xml

* Local Java Class File (Controller.java)

package apps.wcde.components.content.test.mid_hero;

import org.apache.sling.api.resource.Resource;

import org.apache.sling.models.annotations.DefaultInjectionStrategy;

import org.apache.sling.models.annotations.Model;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import javax.inject.Inject;

@Model(

        adaptables = { Resource.class },

        defaultInjectionStrategy = DefaultInjectionStrategy.OPTIONAL

        )

public class Controller implements apps.wcde.components.content.test.mid_hero.Controller {

    private static final Logger logger = LoggerFactory.getLogger(Controller.class);

    @Inject

    protected String title;

    public String getTitle() {

        return title;

    }

}

* htl (mid-hero.html)

<sly data-sly-use.data="apps.wcde.components.content.test.mid_hero.Controller"/>

<div style="font-size: 40px;"> TEST COMPONENT </div>

<div style="font-size: 20px;"> ${data.title} </div>

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

23 likes

Total Posts

52 posts

Correct Reply

8 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile

Avatar
Ignite 1
Level 3
rampai
Level 3

Likes

23 likes

Total Posts

52 posts

Correct Reply

8 solutions
Top badges earned
Ignite 1
Validate 1
Give Back 5
Give Back 3
Give Back 10
View profile
rampai
Level 3

23-08-2019

Hi pdg2491​,pdg2491

I don't think you can include Sling models in components because Sling models are annotation driven POJOs deployed via OSGi bundle.

As stated in the Sling documentation:

The Java Use Provider can be used to load OSGi services, objects exported by bundles or backed by a Resource.

Sling Models Use Provider works in a different manner.

Refer this for more info.

Answers (2)

Answers (2)

Avatar

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,121 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile

Avatar
Coach
Employee
Jörg_Hoh
Employee

Likes

1,089 likes

Total Posts

3,121 posts

Correct Reply

1,063 solutions
Top badges earned
Coach
Give back 600
Ignite 5
Ignite 3
Ignite 1
View profile
Jörg_Hoh
Employee

23-08-2019

as @rampai already pointed out, SlingModels work differently than WcmUsePojo classes, and you need to have more metadata to make them work. Thus you have to deploy them as part of a bundle.

Jörg

Avatar

Avatar
Validate 1
Level 2
pdg2491
Level 2

Likes

5 likes

Total Posts

6 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Applaud 5
View profile

Avatar
Validate 1
Level 2
pdg2491
Level 2

Likes

5 likes

Total Posts

6 posts

Correct Reply

0 solutions
Top badges earned
Validate 1
Boost 5
Boost 3
Boost 1
Applaud 5
View profile
pdg2491
Level 2

26-08-2019

I understood, Thank you for reply