Sightly question: how to adapt a class with a path | Community
Skip to main content
Level 5
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
Community Advisor
VeenaVikramanCommunity AdvisorAccepted solution
Community Advisor
July 26, 2017

Probably my answer would be yes. Try the below code

  Let me know if it doesn't work

Thanks

Veena

BigT168Author
Level 5
July 26, 2017

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.

VeenaVikraman
Community Advisor
Community Advisor
July 26, 2017

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

BigT168Author
Level 5
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.

VeenaVikraman
Community Advisor
Community Advisor
July 26, 2017

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

BigT168Author
Level 5
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.

smacdonald2008
Level 10
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
Level 5
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 {

}

Feike_Visser1
Adobe Employee
Adobe 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
Level 5
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);

.......

....

...

}