JCR API or Apache Sling

I read a lot of articles like JCR vs Apache Sling and I am confused about what to use. Some authors recommend using the JCR API as a more performance-optimized one, and the rest on the Apache Sling side , since it is faster to write and more readable and supported in the long run, And I had some questions:

  • Which practice is best from your point of view?
  • What is most often used in production projects?
+4
source share
2 answers

I think that Maciej Matuszewski has sufficiently exhausted this topic in his speech JCR, Sling or AEM? Which API should I use and when? .

In most cases, it is recommended that you use Apache Sling as a higher API, while JCR is required when performance needs to be taken into account. However, it is important to know the boundary between these two scenarios.

Maciej , 1 AEM . . , , , API-, , , , . , AEM Sling.

, , JCR , CRX, API.

, # ++ - API , , ++.

- .

+3

, "IT DEPENDS" .

:

1: , .

1: API Sling , currentPage, resource, pageManager, wcmmode Java- ( Sling Model/WCMUSe).

// get the page that contains this resource.
// If the resource is a page the resource is returned. Otherwise it 
// walks up the parent resources until a page is found.
Page page = pageManager.getContainingPage(resource);

// Check if the returned page object isn't null
if(page != null){
    return page.getTitle();
}

2: API JCR:

// assign the current resource node to parent Node to check 
// if the current resoure in itself is a page
Node parentNode = currentNode;
while (parentNode.getProperty("jcr:PrimaryTpe").getString() != "cq:Page" ){
    parentNode = parentNode.getParent();
}
// The page Title
String pageTitle = null;

// find the jcr:content node of the page and return the 
// jcr:title property of that node
if(parentNode.hasNode("jcr:content"){
    Node jcrContentNode = parentNode.getNode("jcr:Content");
    pageTitle = jcrContentNode.getProperty("jcr:title").getValue().getString();
} 
 return pageTitle;

, Sling API . API- Sling API JCR.

2. (/content/mywebsite/en, ZERO) -, .

. , JCR, JCR API, Java , , , Sling API .

//Create a connection to the CQ repository running on local host 
Repository repository = JcrUtils.getRepository("http://localhost:4502/crx/server");

//Create a Session
Session session = repository.login( new SimpleCredentials("username", "password".toCharArray()),"crx.default");

//Create a node that represents the root node
Node root = session.getRootNode(); 

// Get the level ZERO node
Node homepageNode = root.getNode("/content/mywebsite/en");

NodeIterator iter = homePageNode.getNodes;
while(iter.hasNext){
    // if next node is of primarty type cq:Page
    // get its jcr:content node and 
    // set its jcr:title property to uppercase letters.
}

:

AEM AEM API Sling API JCR, :

  • API, JCR ( ).
  • .

, , ( , ) Java API JCR.

0

Source: https://habr.com/ru/post/1682503/


All Articles