Questions tagged [sling-models]

Sling Models provide the ability to use annotation driven, "pure" POJOs in Sling-based projects along with dependency injection capabilities and the ability to adapt various Sling objects OOTB.

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. Sometimes these POJOs need OSGi services as well.

Sling Models provide these capabilities as well as basic dependency injection functionality.

The design goals include:

  • Entirely annotation driven. "Pure" POJOs.
  • Use of standard annotations where possible.
  • Pluggability
  • Ability to support resource properties (via ValueMap), SlingBindings, OSGi services, request attributes OOTB
  • Ability to adapt multiple objects - minimal required Resource and SlingHttpServletRequest
  • No need for the client to know/care that these objects are different than any other adapter factory
  • Ability to support both classes and interfaces.
  • Ability to work with existing Sling infrastructure (i.e. not require changes to other bundles).

The bundle is a part of the Apache Sling project ()

Further reading

Similar open-source solutions

  • neba.io - Spring-powered dependency injection brought to Sling
  • Slice - Guice-powered dependency injection with OOTB bindings for Sling and classes
74 questions
6
votes
1 answer

WCM use class can be used wherever we can use sling models.Which one should be preferred and why?

If given an option to use Sling Models or WCM use class which one should be preferred when and why? Is either of them better performance wise? Thanks in Advance
5
votes
3 answers

Get reference to (Mock)ResourceResolverFactory

I have a Sling model class containing a reference to a service of mine: @OSGiService PlanService planService; This service has a reference to ResourceResolverFactory, to get a ResourceResolver with the "admin user": @Reference private…
5
votes
1 answer

How to adapt a child node in sling model of aem6

I am learning to use one of the new features of AEM6 - Sling Models. I have already fetched the properties of a node following the steps described here @Model(adaptables = Resource.class) public class UserInfo { @Inject @Named("jcr:title") …
Manisha Bano
  • 1,853
  • 2
  • 22
  • 34
4
votes
2 answers

Display different markup in Sightly based on a Sling selector

The context I'm working on an AEM 6 project that uses Sightly as the templating language. I'm facing a use case, in which I want to show or hide certain parts of the markup depending on the presence of a Sling selector. For example, a request to…
toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
3
votes
1 answer

@ScriptVariable resourceResolver vs @SlingObject resourceResolver

Can anyone explain to me the difference between calling resourceResolver by annotating @ScriptVariable and by annotating @SlingObject? In what context do they behave differently?
3
votes
2 answers

What Build Module in IntelliJ actually does for maven module

When I run mvn clean install for my maven module then it compiles fine. No issues. But when I open my pom.xml file in IntelliJ and I choose to Build -> Build module then I get following issues: Information:javac 1.8.0_144 was used to compile java…
3
votes
1 answer

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…
iAMintoAEM
  • 71
  • 6
3
votes
1 answer

How to instantiate a Sling Model with multiple adaptables

The @Model annotation in Sling Models allows for multiple adaptables, for example @Model(adaptables = { SlingHttpServletRequest.class, Resource.class }) However, I am not sure how to instantiate a Model with multiple adaptables from a JSP. The…
raro
  • 105
  • 3
  • 9
2
votes
1 answer

Exception Handling in SlingModels and Use Pojos in AEM?

My question is simple. Should we or should we not Handle/Catch Exceptions in Sling Models/WCMUsePojos ? Details: We have several SlingModels which are calling OSGi service methods, when any exception is thrown we are catching it right upto the…
Oliver
  • 6,152
  • 2
  • 42
  • 75
2
votes
1 answer

Get .model.json as String

I wonder if there is an easy way to get a String with the result of a sling content exporter in AEM. In my current usecase I need the content of a component's .model.json output in the component's htl file and sending an additional request is…
Oliver Gebert
  • 1,166
  • 1
  • 6
  • 20
2
votes
1 answer

How to get "Delegation Pattern for Sling Models" working?

I'm trying to create a proxy component for AEM Core Component with custom Sling Model which delegates part of functionality back to the Core Component's Sling Model. I'm following the example from…
arkki
  • 104
  • 6
2
votes
2 answers

Usage of sling model

Which one of the following a better way of defining a sling model and why? @Model(adaptables=Resource.class) public interface MyModel { @Inject String getPropertyName(); } OR @Model(adaptables=Resource.class) public class…
2
votes
0 answers

What might be preventing my Sling model from registering?

My simple Sling model class does not appear in the Apache Sling Console under /system/console/adapters, although the bundle does under /system/console/bundles (and any service classes I annotate with @Component successfully register). I'm using the…
Spinner
  • 732
  • 1
  • 6
  • 21
2
votes
3 answers

SlingModels: Can I inject the SlingHttpServletRequest when adapting from Resource?

I am new to SlingModels and the annotations aren't very clear yet. I am currently trying to transform some basic foundation components from AEM 6.2 to using SlingModels instead. For the image component the foundation JSP uses the…
Thomas
  • 6,325
  • 4
  • 30
  • 65
2
votes
2 answers

SlingModel not mapping the JCR properly

I have a SlingModel called TextModel. @Model(adaptables=Resource.class, defaultInjectionStrategy= DefaultInjectionStrategy.OPTIONAL) public class TextModel { @Inject private String heading; @Inject private String description; …
RK1
  • 429
  • 2
  • 7
  • 28
1
2 3 4 5