Why prefer Sling for building a website and how to get started ?

15 / Feb / 2016 by Arpit Gupta 0 comments

With all the options out there to create a personal website: WordPress, Drupal or Joomla to name a few why would anyone create a site from scratch anymore?

Naturally creating something from scratch takes a lot more work than simply taking something Out of the Box (OOTB).

A number of open source projects allow users to template out pages and do WYSIWYG editing of content but very few give you complete control over how content goes into those templates. Also the concept of making the content modular and reusable is a bit foreign to many platforms.  I wanted to be able to create a site that allowed me to build out content quickly and reuse it anywhere.

Sling makes it very simple to implement simple applications, while providing an enterprise-level framework for more complex applications, which comes with a free data model to store content since it’s built on top of the Java Content Repository (JCR) which stores content in a tree structure which is fitting for a web site.

——————————————————————————————————————————————

Quick Steps for installation and IDE configuration:

You can get started by downloading the Sling jar from the below url

https://sling.apache.org/downloads.cgi#application

To start of with the instance use the below command and it will start your instance in the port 8080 as defined in the properties file of Sling which could be modified as required .
Default login credentials would be username : admin and password : admin .

java -jar org.apache.sling.launchpad-7-standalone.jar

To easily configure the Sling in your IDE , Apache provides a Sling IDE Tooling which would give you with basic functionality to create a Sling Content/Bundle Project  and setting up the server.The tooling kit can be downloaded from the below url :

https://sling.apache.org/downloads.cgi#ide-tooling

After successful installation of IDE Tooling you will be able to create sling project  .To deploy the project on the instance Sling IDE Tooling Kit also provides the feature to for creating a new server to deploy the content on the user specific requirement .

——————————————————————————————————————————————

Magic of POST operation :

Sling also provides a RESTful api for creating, deleting, modifying and transforming content. This means I can create content with simple form posts .

Below is a simple create operation :

<form action="/myresource" method="POST">
  <input type="text" name="username">
  <input type="submit" value="Submit" />
</form>

This would result in the creation of the node under root folder with property username it and would look like this :

Sling    JCR Explorer 1.0.0    Sling    JCR Explorer 1.0.0_

For various other operation you could have a look in the below url

https://sling.apache.org/documentation/the-sling-engine/sling-api-crud-support.html

——————————————————————————————————————————————

Small issues or pain points that I faced are :

First, nothing prevents a user from injecting malicious javascript and html nto the JCR.

For this we can add Anti-Samy to clean the HTML from any javascript and poorly formed html which should prevent most Cross Site Scripting (XSS) attacks.

Second, when we are using these post operation it would always redirect the page on submission to display the status of the POST operation performed as below :

Content modified  myresource
So in-order to redirect it to user-defined path after any CRUD operation we need to add a property as below in our form that should be hidden  :

<input type="hidden" name=":redirect" value="redirect_path" />

 

Thus far I’ve been pretty happy with the result. I have a custom site that gives me the experience I want. Looking forward to continuing to add new features. Enjoy!

FOUND THIS USEFUL? SHARE IT

Leave a comment -