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.
Solved! Go to Solution.
Probably my answer would be yes. Try the below code
Let me know if it doesn't work
Thanks
Veena
Views
Replies
Total Likes
Probably my answer would be yes. Try the below code
Let me know if it doesn't work
Thanks
Veena
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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
Views
Replies
Total Likes
Hi Veena:
this line Article article = articleResource.adaptTo(Article.class); return null; How to find out why adaptTo doesn't work?
Thanks.
Views
Replies
Total Likes
Are you sure Article is a valid Adapter Class. ? Is that a custom adaptable class you have created ?
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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 {
}
Views
Replies
Total Likes
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.
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);
.......
....
...
}
Views
Replies
Total Likes
I think @Model(adaptables = Resource.class) and @Model(adaptables = SlingHttpServletRequest.class) both should work
Views
Replies
Total Likes
I tried, only adaptables = Resource.class works. Other one returns null after articleResource.adaptTo(Article.class);
okay cool. I don't find an option to edit the answer after it is marked correct.
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies
Views
Likes
Replies