Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Best practice to use sling models with WCMUse/WCMUsePojo class

Avatar

Level 7

Hi Team,

I would like to use sling models with WCMUse/WCMUsePojo class. For I have written the below code -

@Model(adaptables = Resource.class)
public class SampleSlingModel extends WCMUse {
    private static Logger LOG = LoggerFactory.getLogger(SampleSlingModel.class);
    @Inject
    private String title;

    @Override
    public void activate() throws Exception {
        LOG.info("SampleSlingModel.activate():Title:" + title);
    }

    public String getTitle() {
        return title;
    }

}

I'm able to get the value of title in component's sightly HTML using get method but the activate method is not invoked. If a class is extending WCMUse should call activate() but it is not. Can you please help me in this?

I tried to put @PostConstruct instead of @Override and activate method is called as sling model is treated it as just another method.

Is it best practice to proceed with this as the methods which are there in WCMUse/WCMUsePojo will not be available in the sling models?

Please suggest.

Thanks,

AryA.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi,

I am not a model expert, but implementing wcmUse is different from Sling Models. You can use both from Sightly (not together), but according to [1] you don't need to have WcmUse and the activate() method at all. I've found [2] which covers the proper usage of wcmUse pojos with Sightly.

Jörg

 

[1] https://sling.apache.org/documentation/bundles/models.html

[2] http://blogs.adobe.com/experiencedelivers/experience-management/sightly-intro-part-4/

View solution in original post

1 Reply

Avatar

Correct answer by
Level 2

Hi,

I am not a model expert, but implementing wcmUse is different from Sling Models. You can use both from Sightly (not together), but according to [1] you don't need to have WcmUse and the activate() method at all. I've found [2] which covers the proper usage of wcmUse pojos with Sightly.

Jörg

 

[1] https://sling.apache.org/documentation/bundles/models.html

[2] http://blogs.adobe.com/experiencedelivers/experience-management/sightly-intro-part-4/