Expand my Community achievements bar.

SOLVED

Sightly question: how to adapt a class with a path

Avatar

Level 6

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Probably my answer would be yes. Try the below code

  Let me know if it doesn't work

Thanks

Veena

View solution in original post

13 Replies

Avatar

Correct answer by
Community Advisor

Probably my answer would be yes. Try the below code

  Let me know if it doesn't work

Thanks

Veena

Avatar

Level 6

That's what I am doing.

Probably I should ask if we can do this in the HTL code

data-sly-use or data-sly-resource?

Thanks for your response.

Avatar

Community Advisor

I am not sure if that is possible, because as far as I could understand, data-sly-resource should refer a resource which will resolve using sling resolution principles . I am not sure if we can refer a page path and try to resolve it as a resource. May be feike_visser​ can help you in this

Thanks

Veena

Avatar

Level 6

Hi Veena:

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

Thanks.

Avatar

Community Advisor

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

Avatar

Level 6

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.

Avatar

Level 10

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.

Avatar

Level 6

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 {

}

Avatar

Employee

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.

Avatar

Level 6

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

.......

....

...

}

Avatar

Community Advisor

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

Avatar

Level 6

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

Avatar

Community Advisor

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