활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.
활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.
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.
해결되었습니다! 솔루션으로 이동.
Probably my answer would be yes. Try the below code
Let me know if it doesn't work
Thanks
Veena
조회 수
답글
좋아요 수
Probably my answer would be yes. Try the below code
Let me know if it doesn't work
Thanks
Veena
조회 수
답글
좋아요 수
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.
조회 수
답글
좋아요 수
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
조회 수
답글
좋아요 수
Hi Veena:
this line Article article = articleResource.adaptTo(Article.class); return null; How to find out why adaptTo doesn't work?
Thanks.
조회 수
답글
좋아요 수
Are you sure Article is a valid Adapter Class. ? Is that a custom adaptable class you have created ?
조회 수
답글
좋아요 수
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.
조회 수
답글
좋아요 수
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.
조회 수
답글
좋아요 수
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 {
}
조회 수
답글
좋아요 수
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);
.......
....
...
}
조회 수
답글
좋아요 수
I think @Model(adaptables = Resource.class) and @Model(adaptables = SlingHttpServletRequest.class) both should work
조회 수
답글
좋아요 수
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.
조회 수
답글
좋아요 수