Monday, May 6, 2013

cq development overview

Create a resource

First, create a resource, typically under /content.
You can use curl, http://localhost:4502/crxde, or http://localhost:4502/libs/wcm/core/content/siteadmin.html.
Let's say you created /content/example/page  node of type cq:Page,  and /content/example/page/jcr:content of type cq:PageContent.

Set type of the resource

Add a property called sling:resourceType for this node: /content/example/page/jcr:content
For example, /content/example/page/jcr:content/sling:resourceType = 'my/base/page'

Define the type

Now, you can define the type, my/base/page,  by creating a scriptlet under /apps/my/base/page/page.jsp  (or /apps/my/base/page/html.jsp . Read this:  http://wem.help.adobe.com/enterprise/en_US/10-0/wem/developing/the_basics.html)

Or, define a servlet and register it with my/base/page.
For example,
import org.apache.felix.scr.annotations.*;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
@Component(immediate = true, metatype = false)
@Service(Servlet.class)
@Properties({@Property(name = "service.description", value = "my example page"),
        @Property(name = "service.vendor", value = "The Apache Software Foundation"),
        @Property(name = "sling.servlet.resourceTypes", value = {"my/base/page"}),
        @Property(name = "sling.servlet.extensions", value = {"html"}),
        @Property(name = "sling.servlet.methods", value = {"GET"})
})
public class BasePageServlet extends SlingAllMethodsServlet {
...
}

 (read: http://sling.apache.org/site/servlets.html  and  http://felix.apache.org/documentation/subprojects/apache-felix-maven-scr-plugin/scr-annotations.html.  )

Make requests to the resource

Now, you can do GET /content/example/page.html .
The request will be handled by /apps/my/base/page/page.jsp  (if you used scriptlet)  or BasePageServlet (if you used provided an OSGi component).

Summary

You create resources. Tag each resource with sling:resourceType, which defines how the resource responds to HTTP requests.


CQ components behave the same way.
Normally, a CQ page is made of many components (resources):
/content/example/page/jcr:content
/content/example/page/jcr:content/image
/content/example/page/jcr:content/text
/content/example/page/jcr:content/par
/content/example/page/jcr:content/par/image
/content/example/page/jcr:content/par/image_0

Each resource has type. If you're using scriptlet, you can use <cq:include> or <sling:include> (read: http://dev.day.com/docs/en/cq/current/howto/taglib.html) to include those child nodes.