【问题标题】:Sling servlet bound to resource Type OSGi R6 annotations does not work绑定到资源类型 OSGi R6 注释的 Sling servlet 不起作用
【发布时间】:2020-08-28 17:35:54
【问题描述】:

我想将一个 servlet 注册到我的主要基本页面资源类型,这样当使用选择器和扩展名访问同​​一页面时,应该选择我的 servlet 而不是正常的页面呈现。

我可以使用旧的 SCR 注释轻松实现此行为。请参阅下面的工作代码。

@Component(metatype = false)
@Service({ javax.servlet.Servlet.class,
        org.apache.sling.api.servlets.SlingSafeMethodsServlet.class })
@Properties({
        @Property(name = "sling.servlet.resourceTypes", value = "project/project-foundation/components/structure/page"),
        @Property(name = "sling.servlet.selectors", value = {"info"}),
        @Property(name = "sling.servlet.extensions", value = "js"),
        @Property(name = "sling.servlet.methods", value = "GET"),
        @Property(name = "service.description", value = "Products Servlet") })

现在我想使用 OSGi R6 注释来做到这一点,下面是我正在使用的注释

@Component(service = Servlet.class, property = { 
        "sling.servlet.selectors=info",
        "sling.servlet.extensions=js",
        "sling.servlet.methods="+ HttpConstants.METHOD_GET,
        "sling.servlet.resourceTypes=project/project-foundation/components/structure/page"
})

当我点击页面 /en-us/fun-games.info.js 时,我的 servlet 应该被调用,它现在正在打印 hello world 字符串。(如果调用)

当我使用 OSGi R6 注释时,当我使用 selector = info 和 extension = js 访问上述 URL 时,没有选择这个 servlet。

但这适用于旧的 SCR 注释。我检查了我的 servlet 是否已正确注册。

不知道发生了什么。

注意:我使用的是 AEM 6.5

我提到了这个Link for OSGi R6

【问题讨论】:

  • 您是否还更新了 pom.xml 以使用 maven-bundle-plugin 3.2.0 或更高版本。 OSGi R6 注释需要它。
  • 嗨 @rakhi4110 我的 maven-bundle-plugin 版本是 <plugin> <groupId>org.apache.felix</groupId> <artifactId>maven-bundle-plugin</artifactId> <version>4.1.0</version> <inherited>true</inherited> </plugin>

标签: aem apache-felix sling aem-6


【解决方案1】:

天哪,这比我想象的要难。

我使用 OSGi R7 注释解决了这个问题。我不知道它们存在并且 AEM 支持它。

@Component(immediate = true,service = { Servlet.class })
@SlingServletResourceTypes(
    resourceTypes="project/project-foundation/components/structure/page", 
    methods= "GET",
    selectors={ "info", "js"})

另外需要注意的是,我使用“js”作为选择器而不是扩展。

我以这种方式调用页面 /zh-CN/fun-games/mario.info.js

可以在完整或缩短的 URL 上调用相同的 servlet,这无关紧要。

还有一个依赖:

           <dependency>
            <groupId>org.apache.sling</groupId>
            <artifactId>org.apache.sling.servlets.annotations</artifactId>
            <version>1.2.4</version>
          </dependency>

我提到的官方文档是OSGi R7 Anoted Servlets

【讨论】:

    【解决方案2】:

    尝试在路径中提供 JCR 内容时点击 servlet,例如:

    en-us/fun-games/_jcr_content.info.js
    

    我遇到了同样的问题,但是当我在路径中提供 JCR 内容时,它起作用了。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-17
      • 2014-06-03
      • 2017-04-07
      • 2014-06-04
      • 1970-01-01
      • 2018-04-27
      • 2018-08-31
      相关资源
      最近更新 更多