What's the purpose of resourceResolver.adaptTo(Session.class) in Apache Sling?

≯℡__Kan透↙ 提交于 2019-12-12 10:41:53

问题


I am new to Apache Sling, CQ5, etc.

In our codebase, we have a code snippet similar to this:

void perform(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    ResourceResolver resourceResolver = request.getResourceResolver();

    Session session = resourceResolver.adaptTo(Session.class);
    PageManager pageManager = resourceResolver.adaptTo(PageManager.class);
}

What's adapTo doing here?

Also is there a good documentation/user manual available I can read to get started using Sling, CQ5, etc.?


回答1:


The adaptTo() method found in many sling objects allows to "transform" objects. Sling could have decided to add a resolver.getSession() method, but that wouldn't have been very flexible. The nice thing about adaptTo is that is it dynamic. you can create adapters to transform between different types (they are OSGi services). Sling and CQ5 also include bunch of adapters by default. The sling Wiki has some docs about adapters.

About how to start with Sling and CQ5, the sling site is a good place to start



来源:https://stackoverflow.com/questions/21715989/whats-the-purpose-of-resourceresolver-adapttosession-class-in-apache-sling

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!