集成 AEM/CQ5 和 Spring 框架?

Intergrating AEM/CQ5 and Spring Framework?

我一直在尝试将 Spring 框架集成到 AEM6/CQ5。

我正在学习本教程。 LINK

根据教程,我已经安装了 NEBA 包。 所有 NEBA 包在 OSGi 控制台中都处于活动状态。

然后我创建了自己的Maven CQ5项目,添加了Neba注解和Spring的依赖。我的项目也在 CQ5 中成功部署(bundle is active)。

我尝试使用 NEBA 的 ResourceModel 注释。但是这个模型并没有出现在 NEBA 的模型注册表中。 我将 ResourceModel 映射到我创建的内容组件 "linkComponent"。

当用户将其拖放到任何 parsys 上时,资源节点具有属性 linkNamelinkURL.

我尝试在 JSP 中访问这些值,但失败了。

请看下面的代码: 包裹 com.zensar.neba;

import org.apache.sling.api.resource.Resource;

import io.neba.api.annotations.Path;
import io.neba.api.annotations.ResourceModel;

@ResourceModel(types = "zensar-neba/components/content/linkComponent")
public class LinkComponent {

    private String linkName;
    private String linkURL;

    public String getLinkName() {
        return linkName;
    }
    public void setLinkName(String linkName) {
        this.linkName = linkName;
    }
    public String getLinkURL() {
        return linkURL;
    }
    public void setLinkURL(String linkURL) {
        this.linkURL = linkURL;
    }


}

请参阅下面JSPlink组件的代码:

<%@include file="/libs/foundation/global.jsp"%>
<%@taglib prefix="neba" uri="http://neba.io/1.0"%>
<neba:defineObjects />
Link Component
<a href="${m.linkURL}"> Click Here ${m.linkName}</a>

然后我尝试使用 Spring 注释 创建一个 Controller 但我得到了 "Path not found"我错过了什么。

请看下面的代码:

package com.zensar.neba;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class DemoController {
    @RequestMapping("/echo/{param}")
    @ResponseBody
    public String echo(@PathVariable("param") String paramToEcho) {
       return "Hello "+ paramToEcho;
    }
}

我这样调用控制器 link:http://localhost:4502/bin/mvc.do/echo/Oliver

重要说明: 我所有的捆绑包都有效

如果所有包都已启动并且 运行 并且您看到了模型注册表,则意味着一切都 运行 顺利。但是:您的模型没有出现,这意味着您的包的应用程序上下文不是 运行。 NEBA 使用 gemini-blueprint,OSGi 蓝图规范的参考实现。

你有 xml 文件吗? blueprint.xml,在您的包的 OSGI-INF/blueprint 文件夹中?它应该是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:bp="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
   http://www.osgi.org/xmlns/blueprint/v1.0.0
   http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.2.xsd">

    <context:component-scan base-package="com.zensar.neba" />
</beans>

如果您这样做,您的 error.log 在您的捆绑包启动时会说什么? 作为参考,这是一个使用 NEBA 的示例 Web 应用程序:https://github.com/unic/publication-neba-adaptto-2015

还有一件事:NEBA 使用私有字段注入,所以你不需要在你的 bean 上有任何 setter。

很高兴能帮到您!只是回答你的最后一个问题:在 Maven 项目中,蓝图 XML 文件的正确位置将在包含模型/控制器的 Maven 模块的 src/main/resources/OSGI-INF/blueprint 文件夹中。