吊索资源与节点 [英] Sling resource vs nodes

查看:49
本文介绍了吊索资源与节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解为什么您会在吊索中使用资源而不是节点.所以说我有一些简单的访问节点,如下所示:

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.

API与JCR节点API有所不同,但是使用了相似的概念.使用Resources绝对简单的一件事是访问属性值,因为您在ValueMap中获取它们,而丢失的属性不会抛出异常.例如,

The API is somewhat different from the JCR nodes API, but uses similar concepts. 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.

以上文档应解释主要模式,简而言之是:

The above docs should explain the main patterns, in short those are:

  • 您可以从Sling请求中获取资源,或使用ResourceResolver服务
  • 资源可以适应ValueMap以访问其属性
  • 如果您需要切换到JCR API,则资源可以适应节点
  • Resource.listChildren(...)类似于Node.getNodes()
  • Resource.getResourceResolver()提供了一个ResourceResolver,它可以通过搜索或路径来访问其他资源.

该资源的存在是为了对内容存储进行抽象化,以便可以使用Sling中除JCR之外的其他后端,并统一Sling对其内部使用的数据和内容的看法.

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.

对于应用程序级编程,我认为JCR API非常好,我不会仅出于此目的而使用Resource.但是在某些情况下,Resource API使事情变得更简单.

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. But there are some cases where the Resource API makes things simpler.

这篇关于吊索资源与节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆