简体   繁体   English

replaceSelector和include文件之间的区别

[英]Difference between replaceSelector and include file

I am writing JSP code and I have included sling taglibs as 我正在编写JSP code并且包含了sling taglibs作为

<sling:defineObjects/>

What is the best way to include an external file ? 包括外部文件的最佳方法是什么?

There are two options: 有两种选择:

  1. Include file: <%@ include file="global.jsp" %> 包含文件: <%@包含文件=“ global.jsp”%>
  2. Use replaceSelectors as <sling:include replaceSelectors="global" /> replaceSelectors用作<sling:include replaceSelectors="global" />

Both are working fine except I am not getting sling request object in global.jsp when I use include file option. 两者都工作正常,但当我使用包含文件选项时,在global.jsp中没有得到吊索请求对象。 I want to know more details. 我想知道更多细节。

So this works only under a certain condition where you have a global file in the same location as the jsp that you are writing. 因此,这仅在特定条件下有效,即在与要编写的jsp相同的位置具有全局文件的情况下。

Your first option is including the file global.jsp inline at the time of compilation. 您的第一个选择是在编译时内联包含文件global.jsp。 This is then fixed and if you change something in global.jsp it won't be pulled in unto you recompile this servlet. 然后,此问题已解决,如果您在global.jsp中进行了更改,则不会重新编译该servlet。

The second option you listed is taking the existing resource request and changes out the selector, if any, with "global." 您列出的第二个选项是获取现有资源请求,并使用“全局”更改选择器(如果有)。 It then runs this through the sling resolution process to determine which file to include. 然后,它在吊索解析过程中运行此流程,以确定要包含的文件。

Since the global.jsp, in the same directory, will match your request, it's included. 由于位于同一目录中的global.jsp将匹配您的请求,因此将其包括在内。

See Sling URL Resolution for more details, in particular the Examples section which will show how the selector would indicate the jsp to use. 有关更多详细信息,请参见Sling URL解析 ,尤其是“ 示例”部分,该部分将显示选择器如何指示要使用的jsp。

Note: as I mentioned this works, because the resolver resolves the global in this case. 注意:正如我提到的那样,这是可行的,因为在这种情况下,解析程序会解析全局变量。 If the global.jsp you were attempting to add was in a different directory structure it would not work. 如果您尝试添加的global.jsp位于不同的目录结构中,则它将无法正常工作。

Now to answer your specific question as to which is better. 现在回答关于哪个更好的特定问题。 It depends. 这取决于。 If you are developing in sling then the sling include is considered the better option as it responds better when the identified resource is changed. 如果您正在使用吊索进行开发,则吊索包含被认为是更好的选择,因为在更改标识的资源时,它的响应更好。

I should note that the specific usage of sling include is not the normal usage for global.jsp as the global file is usually in a fixed position and you want to use that specific version. 我应该注意,吊索包含的特定用法不是global.jsp的常规用法,因为全局文件通常位于固定位置,并且您想使用该特定版本。

The more common usage of the sling include is to specify the path and/or resource. 吊索的更常见用法是指定路径和/或资源。

The interesting usage of your option 2 is that the actual file that is resolved is relational to the component and is determine through the sling resolution, so you can extend or override this component and include a new global.jsp that would be utilized. 选项2的有趣用法是,解析的实际文件与组件相关,并通过吊索分辨率确定,因此您可以扩展或覆盖此组件,并包括将要使用的新global.jsp。

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

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