简体   繁体   English

吊索资源与节点

[英]Sling resource vs nodes

I'm having trouble understanding why you would use resources instead of nodes in sling. 我无法理解为什么您会在吊索中使用资源而不是节点。 So say I have something simple accessing nodes like below: 所以说我有一些简单的访问节点,如下所示:

NodeIterator headerNode = currentNode.getNodes();
//loop through and do something with the nodes.

How would you work in resources instead of nodes. 您将如何处理资源而不是节点。 I've heard you should generally work in resources in sling not nodes. 我听说您通常应该使用吊索而不是节点来处理资源。 But why? 但为什么? I really don't understand what the benefit to this would be. 我真的不明白这样做会有什么好处。 I think I'm having trouble grasping what resources are as well. 我想我也很难掌握什么资源。 I know there's documentation but I can't find any code samples on how to use them. 我知道有文档,但是找不到有关如何使用它们的代码示例。

The main documentation to look at is http://sling.apache.org/documentation/the-sling-engine/resources.html which explains the Resource concept and how you work with them. 要查看的主要文档是http://sling.apache.org/documentation/the-sling-engine/resources.html ,其中说明了资源概念以及如何使用它们。

The API is somewhat different from the JCR nodes API, but uses similar concepts. 该API与JCR节点API有所不同,但是使用相似的概念。 The one thing which is definitely simpler with Resources is accessing property values, as you get them in a ValueMap and missing properties don't throw exceptions for example. 使用资源显然更简单的一件事是访问属性值,因为您在ValueMap中获取它们,而缺少的属性不会抛出异常。

The above docs should explain the main patterns, in short those are: 上面的文档应该解释主要模式,简而言之是:

  • You get a Resource from the Sling Request, or using a ResourceResolver service 您从吊索请求中获取资源,或使用ResourceResolver服务
  • A Resource can be adapted to a ValueMap to access its properties 可以将资源适应ValueMap以访问其属性
  • A Resource can be adapted to a Node if you need to switch to the JCR API 如果需要切换到JCR API,可以将资源适应节点
  • Resource.listChildren(...) is similar to Node.getNodes() Resource.listChildren(...)与Node.getNodes()类似
  • Resource.getResourceResolver() provides a ResourceResolver that gives access to other Resources by searching or by path. Resource.getResourceResolver()提供了一个ResourceResolver,它可以通过搜索或按路径访问其他资源。

The Resource exists to abstract the content storage, to make it possible to use other backends than JCR in Sling and to unify Sling's view on the data and content that it uses internally. 资源的存在是为了对内容存储进行抽象化,以便可以在Sling中使用除JCR之外的其他后端,并统一Sling在其内部使用的数据和内容的视图。

For application-level programming, in my opinion the JCR API is very nice, I wouldn't use Resource instead just for the sake of it. 在应用程序级编程中,我认为JCR API非常好,我不会仅出于此目的而使用Resource。 But there are some cases where the Resource API makes things simpler. 但是在某些情况下,Resource API使事情变得更简单。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM