英文:
Manage two identical servlets via path in AEM by Sling
问题
I have two projects in the same AEM instance, and in both projects I need the same servlet (I don't want to change selector), but it has to handle different templates.
在同一个AEM实例中有两个项目,在这两个项目中都需要相同的Servlet(我不想改变选择器),但它需要处理不同的模板。
By deploying both projects, calling their respective resources with the servlet, one answers and the other does not. This is because the same servlet is always called in each case (and therefore does not manage the templates of a project).
通过部署这两个项目,并使用Servlet调用它们各自的资源,一个能够正常响应,而另一个不能。这是因为在每种情况下都调用了相同的Servlet(因此无法管理项目的模板)。
So I would like to understand if it is possible to manage the two servlets by adding a control on an array of base path. In this way each project goes into the correct servlet and all the templates are managed.
因此,我想了解是否可以通过在基本路径数组上添加控制来管理这两个Servlet。这样,每个项目都进入正确的Servlet,所有模板都得以管理。
Example:
示例:
Current version servlet project A
项目A当前版本的Servlet
Current version servlet project B
项目B当前版本的Servlet
New version servlet project A
项目A的新版本Servlet
New version servlet project B
项目B的新版本Servlet
英文:
I have two projects in the same AEM instance, and in both projects I need the same servlet (I don't want to change selector), but it has to handle different templates.
By deploying both projects, calling their respective resources with the servlet, one answers and the other does not. This is because the same servlet is always called in each case (and therefore does not manage the templates of a project).
So I would like to understand if it is possible to manage the two servlets by adding a control on an array of base path. In this way each project goes into the correct servlet and all the templates are managed.
Example:
Current version servlet project A
@Component(
service = {Servlet.class},
property = {
Constants.SERVICE_DESCRIPTION + "=Menu Servlet",
SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
SLING_SERVLET_RESOURCE_TYPES + "=" + NameConstants.NT_PAGE,
SLING_SERVLET_SELECTORS + "=" + "contents",
SLING_SERVLET_EXTENSIONS + "=json"
}
)
Current version servlet project B
@Component(
service = {Servlet.class},
property = {
Constants.SERVICE_DESCRIPTION + "=Servlet",
SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
SLING_SERVLET_RESOURCE_TYPES + "=" + NameConstants.NT_PAGE,
SLING_SERVLET_SELECTORS + "=" + "contents",
SLING_SERVLET_EXTENSIONS + "=json"
}
)
New version servlet project A
@Component(
service = {Servlet.class},
property = {
Constants.SERVICE_DESCRIPTION + "=Servlet",
SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
SLING_SERVLET_RESOURCE_TYPES + "=" + NameConstants.NT_PAGE,
SLING_SERVLET_SELECTORS + "=" + "contents",
SLING_SERVLET_EXTENSIONS + "=json",
--> NEW_SELECTOR + "=[/content/progA, /content/experience-fragments/progA]"
}
New version servlet project B
@Component(
service = {Servlet.class},
property = {
Constants.SERVICE_DESCRIPTION + "=Servlet",
SLING_SERVLET_METHODS + "=" + HttpConstants.METHOD_GET,
SLING_SERVLET_RESOURCE_TYPES + "=" + NameConstants.NT_PAGE,
SLING_SERVLET_SELECTORS + "=" + "contents",
SLING_SERVLET_EXTENSIONS + "=json",
--> NEW_SELECTOR + "=[/content/progB,/content/experience-fragments/progB]"
}
</details>
# 答案1
**得分**: 2
你可以实现[OptingServlet接口][1],并定义`accepts`方法,该方法可以通过某种机制(例如,通过请求参数)决定是否应该使用当前的Servlet来处理请求,如果不是,则请求将传递给下一个候选的Servlet。
我还想指出,这是一个相当奇怪的用例,通常您会希望在不同项目中为Servlet使用非干扰路径模式。还要避免使用不必要宽泛的资源类型,如`NT_PAGE`或`cq:Page`,这会使Servlet成为系统中所有页面的候选项,可能对性能产生不利影响。
[1]: https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/javadoc/org/apache/sling/api/servlets/OptingServlet.html
<details>
<summary>英文:</summary>
you can implement [OptingSevlet interface][1] and define the `accepts` method that can decide using some mechanism (e.g. through a request param), whether the current servlet should be used to service the request, if not the request will go to the next candidate servlet.
I would also like to point out this is a rather odd usecase, usually you would want to use non-interfering paths pattern for mounting servlet in different projects. also avoid using unnecessarily wide resource types like `NT_PAGE` or `cq:Page`, this makes the servlet a candidate for all pages in the system and can have a detrimental effect on performance.
[1]: https://helpx.adobe.com/experience-manager/6-5/sites/developing/using/reference-materials/javadoc/org/apache/sling/api/servlets/OptingServlet.html
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论