Latest Posts

Archives [+]

Categories [+]

Authors [+]

Another Sling Sample App

Here's another little example application that demonstrates how to use Apache Sling (and its underlying Java Content Repository). The app is called "Notes". It allows users to save a written note and attach a zip file to it. Notes can be edited which creates a new version (old versions remain in the repository). They can also be commented upon in a hierarchical way (i.e. comments can get comments again). Comments are also versioned. Finally, you can search through existing notes.

The app is packaged with CRX Quickstart's Package Manager. In order to install it just fire up the Package Manager and select Upload Package. This will install the scripts at /apps/notes as well as some initial content. After that just hit http://localhost:7402/content/notes.html.

Notes is designed to show off a number of JCR/Sling features like hierarchical storage, binary data storage, search, versioning and Ajax (via JSON). The latter will be visible in the search results page: the Quick View link that appears behind a search result retrieves the comments thread on the fly using Sling's built-in JSON renderer (try search term "jcr"). Comment versioning will become apparent when you edit an existing comment: there will be a new link "old versions".

A quick tour of the code

The front page is rendered by html.jsp. In there, child nodes are retrieved and rendered directly (by accessing their properties) The real Sling way is shown in the thread view (file thread.jsp): child nodes are rendered by using sling.include and the selector "detail". For this, the file detail.jsp is invoked recursively down the comment tree.

Posting new threads and comments is handled by POST.jsp. I decided to implement this a) for instructive reasons and b) because I wanted all nodes to be versionable (so I add the mixins mix:versionable and mix:referenceable to new nodes).

search.jsp demonstrates how to perform a search over the content repository and how to retrieve and use nodes via JSON. The URL /path/to/node.-1.json will rerurn the full sub tree below a node.

I gave up on my attempt to write a css file. The one that is included is from bluerobot.com. If you want to delete the initial content make sure that there is still a node left at /content/notes. This is assumed by the app (like many other things). In fact, this is not to be considered a real app, but just a sample to get you started with content-centric apps on Sling. Hence, it includes some sub-optimal aspects (feel free to fix them...): node UUIDs are exposed as anchors, node names are ugly and file uploads are not mime-type aware (there is probably more).

 
(optional)
8 comments
Nice sample code, including XPATH queries. I wouldn't call it elegant, rather JSP-vanilla, but it's good to have a sample from the sling source anyway!

Thanks to Michael!
0 Replies » Reply
this just looks like JSP together with JCR...
0 Replies » Reply
WOW! nice tutorial! that's what I exactly need. You are explaining in such a simple and straightforward! why others can't do it as you've done? may be it's a talent... thanx a lot!
0 Replies » Reply
Hi Michael,

does the example application also work with plain sling (without the CRX package manager)? If yes, how do I install it?

TIA!
0 Replies » Reply
I just found the corresponding patch in the Sling issue tracker and go on from there.
0 Replies » Reply
Hi Andreas,
yup, here's the best place to look at if you want to run the app in Sling: https://issues.apache.org/jira/browse/SLING-572
Michael
0 Replies » Reply
Is there any way to make new nodes versionables using the SlingPostServlet?

I don't want to overwrite the POST.

Any idea?
0 Replies » Reply

Resource dumped by HtmlRendererServlet

Resource path: /content/usergenerated/content/ddc/blog/2008/07/anothersampleapp/jcr:content/comments/HiMichaelb

Resource metadata: {sling.resolutionPath=/content/usergenerated/content/ddc/blog/2008/07/anothersampleapp/jcr:content/comments/HiMichaelb}

Resource type: cq:Comment

Resource super type: -

Resource properties

approved: true
jcr:mixinTypes: [cq:ReplicationStatus]
jcr:createdBy: admin
cq:lastReplicatedBy: admin
jcr:created: java.util.GregorianCalendar[time=1263901551153,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=0,WEEK_OF_YEAR=4,WEEK_OF_MONTH=4,DAY_OF_MONTH=19,DAY_OF_YEAR=19,DAY_OF_WEEK=3,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=6,HOUR_OF_DAY=6,MINUTE=45,SECOND=51,MILLISECOND=153,ZONE_OFFSET=-18000000,DST_OFFSET=0]
cq:lastReplicated: java.util.GregorianCalendar[time=1266246981525,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id="GMT-05:00",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2010,MONTH=1,WEEK_OF_YEAR=8,WEEK_OF_MONTH=3,DAY_OF_MONTH=15,DAY_OF_YEAR=46,DAY_OF_WEEK=2,DAY_OF_WEEK_IN_MONTH=3,AM_PM=0,HOUR=10,HOUR_OF_DAY=10,MINUTE=16,SECOND=21,MILLISECOND=525,ZONE_OFFSET=-18000000,DST_OFFSET=0]
cq:lastReplicationAction: Activate
jcr:primaryType: cq:Comment