Sightly question: how to adapt a class with a path | Community
Skip to main content
New Participant
July 26, 2017
Solved

Sightly question: how to adapt a class with a path

  • July 26, 2017
  • 13 replies
  • 6741 views

In java code, I can do this:

Resource articleResource = resourceResolver.getResource("/somepath/for/article");
  Article article = articleResource.adaptTo(Article.class);

Can Sightly do the same? and how?

In AEM 6.3 with sling models.

Thanks.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by VeenaVikraman

Probably my answer would be yes. Try the below code

  Let me know if it doesn't work

Thanks

Veena

13 replies

VeenaVikraman
New Participant
July 26, 2017

okay cool. I don't find an option to edit the answer after it is marked correct.

BigT168Author
New Participant
July 26, 2017

I tried, only adaptables = Resource.class works. Other one returns null after articleResource.adaptTo(Article.class);

VeenaVikraman
New Participant
July 26, 2017

I think @Model(adaptables = Resource.class) and @Model(adaptables = SlingHttpServletRequest.class) both should work

BigT168Author
New Participant
July 26, 2017

Sorry, the correction answer should be

@Model(adaptables = Resource.class)

public class Sample {

private final Logger LOG = LoggerFactory.getLogger(getClass());

@Inject

SlingHttpServletRequest request;

@PostConstruct

protected void init() {

try {

Resource articleResource = request.getResourceResolver().getResource("/somepath/for/article");
  Article article = articleResource.adaptTo(Article.class);

.......

....

...

}

Feike_Visser1
Employee
July 26, 2017

What you want still requires some code now, I did raise this request to have it more flexible to adapt objects in HTL.

[SLING-6504] Use-api to support more flexible way to adapt from different objects - ASF JIRA

From AEM6.3 you can use data-sly-use to get the resource, but you still need some code to adapt.

BigT168Author
New Participant
July 26, 2017

If I change the Article to adaptables to Resource.class, this line is fine

Article article = articleResource.adaptTo(Article.class)

The Article.java code is changed to below, other parts are the same.

@Model(adaptables=Resource.class)

public class Article {

}

smacdonald2008
New Participant
July 26, 2017

Although the front end is JSP - the backend shows you how to adapt and use Sling Models -- see:

Adobe Experience Manager Help | Creating Adobe Experience Manager 6.3 Sling Model Components

As you can see - this shows how to adapt a custom class with @Model.

BigT168Author
New Participant
July 26, 2017

Yes, this is a custom adaptable class.

@Model(adaptables = SlingHttpServletRequest.class, adapters = Article.class, resourceType = ArticleImpl.RESOURCE_TYPE)

@Exporter(name = Constants.EXPORTER_NAME, extensions = Constants.EXPORTER_EXTENSION)

public class ArticleImpl implements Article {

@ScriptVariable

    private ValueMap properties;

    @ScriptVariable

    private Style currentStyle;

    @ScriptVariable

    private Page currentPage;

    @SlingObject

    private ResourceResolver resourceResolver;

    @SlingObject

    private Resource resource;

    @Self

    private SlingHttpServletRequest request;

//some fields definition

@PostConstruct

    private void init() {

    tagManager = resourceResolver.adaptTo(TagManager.class);

    fileReference = properties.get("fileReference", "");

    populatePrimaryTag();

    populateArticleDate();

    populateAuthors();

    }

//some methods

}

Anything particular I missed?

Thanks a lot.

VeenaVikraman
New Participant
July 26, 2017

Are you sure Article is a valid Adapter Class. ? Is that a custom adaptable class you have created ?

BigT168Author
New Participant
July 26, 2017

Hi Veena:

this line Article article = articleResource.adaptTo(Article.class); return null; How to find out why adaptTo doesn't work?

Thanks.