Sling Servlet中的CQ5 QueryBuilder参考 [英] CQ5 QueryBuilder Reference in Sling Servlet

查看:109
本文介绍了Sling Servlet中的CQ5 QueryBuilder参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在声明一个像这样的sling servlet

I am declaring a sling servlet like so

@Component(metatype = false)
@Service(Servlet.class)
@Properties({
        @Property(name = "sling.servlet.paths", value = "/bin/foo/bar"),
        @Property(name = "sling.servlet.methods", value = "POST") })
public class FooBarServlet extends SlingAllMethodsServlet {
    ...
}

我像这样覆盖doPost

I override doPost like so

@Override
    protected void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) throws IOException {
    ...
}

我可以从客户端发帖。太好了!

And I am able to post from a client. Great!

我扔了以下

@Reference
private QueryBuilder queryBuilder;

根据文档,应插入对查询生成器的引用。但这似乎并非如此。在日志中,我看到此错误

as per the documentation, a reference to query builder should be injected. But it does not seem to. In the log I see this error

bindQueryBuilder cannot be found (java.lang.VerifyError: ...

当我尝试发布到servlet时,我得到了这个

And when I try to post to the servlet I get this

javax.jcr.RepositoryException: org.apache.sling.api.resource.PersistenceException: Resource at '/bin/foo/bar' is not modifiable.

在OSGi控制台中,我看到我的软件包已安装,这就是我的servlet的意思

And in the OSGi console I see my bundle is installed, and this is what it has to say about my servlet

Service ID 3075 Types: javax.servlet.Servlet
Service PID: com.myproject.FooBarServlet
Component Name: com.myproject.FooBarServlet
Component ID: 5526
Vendor: Adobe

关于我在做什么错?

推荐答案

我一直在使用教程作为参考。
我遇到了

I had been using this tutorial as a reference. I came across this about the Felix Service Component Runtime (SCR)

,因此我实现了以下

protected void activate(ComponentContext context) {
    LOGGER.info("activating {}", this.getClass().getName());
}

protected void unbindQueryBuilder(QueryBuilder queryBuilder) {
    this.queryBuilder = null;
}

protected void bindQueryBuilder(QueryBuilder queryBuilder) {
    this.queryBuilder = queryBuilder;
}

,它起作用了!因此,通过更深入的研究,我了解到这些绑定/解除绑定方法实际上应该是由maven-scr-plugin生成的,我的版本为1.6.0

and it worked! So upon closer investigation I learned that these bind/unbind methods are actually supposed to be generated by the maven-scr-plugin, of which I have version 1.6.0

            <plugin>
                <groupId>org.apache.felix</groupId>
                <artifactId>maven-scr-plugin</artifactId>
                <version>1.6.0</version>
                <executions>
                    <execution>
                        <id>generate-scr-scrdescriptor</id>
                        <goals>
                            <goal>scr</goal>
                        </goals>
                        <configuration>
                            <!-- Private service properties for all services. -->
                            <properties>
                                <service.vendor>Adobe</service.vendor>
                            </properties>
                        </configuration>
                    </execution>
                </executions>
                <dependencies>
                    <dependency>
                        <groupId>org.slf4j</groupId>
                        <artifactId>slf4j-simple</artifactId>
                        <version>1.5.2</version>
                    </dependency>
                </dependencies>
            </plugin>

对于注释我有1.4.0

and for the annotations I have 1.4.0

            <dependency>
                <groupId>org.apache.felix</groupId>
                <artifactId>org.apache.felix.scr.annotations</artifactId>
                <version>1.4.0</version>
                <scope>provided</scope>
            </dependency>

所以虽然我不确定为什么没有生成bind / unbind方法,但我知道它们应该是这样,所以我手动生成它们。

so although I am not sure why the bind/unbind methods are not getting generated, I know that they should be, so I generate them manually.

更新
我试图将maven-scr-plugin更新到1.20版。 0,在mvn构建期间产生以下错误

Update I tried to update the maven-scr-plugin to version 1.20.0, which yielded the following error during mvn build

[ERROR] Project depends on org.apache.felix:org.apache.felix.scr.annotations:jar:1.4.0:provided
[ERROR] Minimum required version is 1.9.0

所以...我将org.apache.felix.scr.annotations更新为1.9.0。而且有效!我的bind / unbind访问器已生成,一切都很好。但是,我很担心,不知道是否应该使用org.apache.felix.scr.annotations的1.9.0版本,因为我在行家中将其标记为提供的依赖性,当我查看安装在cq实例上的OSGi捆绑包时,会看到以下内容

so... I updated the org.apache.felix.scr.annotations to 1.9.0. And it works! My bind/unbind accessors are generated and all is great. However, I am concerned and do not know if I should use version 1.9.0 of org.apache.felix.scr.annotations because I am marking it as provided in the maven dependency and when I look at the OSGi bundles installed on the cq instance I see the following

Apache Felix Declarative Services (org.apache.felix.scr) : Version 1.6.3.R1409029

这篇关于Sling Servlet中的CQ5 QueryBuilder参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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