2
votes

Can any one tell me the right way to start ,configure as well as do CRUD operation for sling. I followed the tutorial below but it was not for CRUD.

http://sling.apache.org/documentation/getting-started/discover-sling-in-15-minutes.html

Can any one share there experience with Sling framework?

Only experienced people allowed (Who has worked with Sling).

3
What do you mean "it was not for CRUD"? The answer below has exactly same content as url you provided.Dawid Pura

3 Answers

5
votes

CRUD operations are done with HTTP requests. I'll use curl in my examples, and operate on a node at http://localhost:8080/content/mynode, since that's what is being used in the tutorial you have read.

Create: To create content you do a HTTP POST request. In the tutorial the following example is used:

curl -u admin:admin -F"sling:resourceType=foo/bar" -F"title=some title" http://localhost:8080/content/mynode

This creates new content at http://localhost:8080/content/mynode, with a single property title=some title

Read: Simply to a GET request to the content node:

curl http://localhost:8080/content/mynode

... or even simpler, use your web browser and navigate to http://localhost:8080/content/mynode

Update: You can also update with a POST request, e.g.:

curl -u admin:admin -F"sling:resourceType=foo/bar" -F"title=some other title" http://localhost:8080/content/mynode

This sets the title of the already existing content node to some other title. You can also add new properties:

curl -u admin:admin -F"sling:resourceType=foo/bar" -F"myProperty=my value" http://localhost:8080/content/mynode

... which adds the property myProperty, with the value my value.

Delete: You delete a content node by sending an HTTP DELETE request:

curl -u admin:admin -X DELETE http://localhost:8080/content/mynode

For the full range of operations you can do, see Apache Sling: Manipulating Content - The SlingPostServlet

2
votes

Apart from what Vidar S. Ramdal said, you can implement CRUD operations in Java utilising the org.apache.sling.api.resource.ResourceResolver API, provided as OSGi service. That is the general approach to created content programmatically. If exceptions have to be made (i.e. old Sling version ) for whatever reason, the JSR 170 API (Java Content Repository) can be used.

Using adapters, you can easily transform a Resource (code Sling type representing content) into a node.

0
votes

It strongly depends on what you really want to do. If you want to enable your enduser with the option to write a comment to your blog post for example. You can use the following form.

<form method="POST" action="/content/current/post/path" >
   <input type="text" name="headline" value="" />
   <input type="text" name="text" value="" />
</form>

Another option ist the content manipulation by using the post engine like the others above already described.

You can also use Java code by resolving the resource by path using the ResourceResolver. https://sling.apache.org/documentation/tutorials-how-tos/getting-resources-and-properties-in-sling.html Another options is to use the nodes in the repository directly by using resource.adaptTo(Node.class)

BR Tim