I'm just getting started with Sling Launchpad and have grasped the basic concepts of selectors and so forth and have been able to read the contents of nodes. However, I can't figure out how to create a new node.
I have a JSP in my jackrabbit repository which is processing correctly when I access it using a web browser.
I wish to perform an action that will create a new node in the /content/myappname tree when the page is loaded.
How do I go about doing this? I figure I need to create a Session object in my code and log that Session is as an admin user and then perform the node creation. I'm just not sure what objects to use out of what I have available to me in that scope.
Can anyone provide me with a quick code example of how to create a node from within a JSP?
You can get a get an admin## session through SlingRepsitory object. From the session you can use jack rabbit api to create nodes.
<sling:defineObjects>
tag exposes sling variable (you can include global.jsp, which exposes all these variables). Sling's getService() method can be used to get an instance of SlingRepository.
org.apache.sling.jcr.api.SlingRepository repos = sling.getService(SlingRepository.class);
javax.jcr.Session session = repos.loginAdministrative(null);
javax.jcr.Node root = session.getRootNode();
javax.jcr.Node newNode = root.addNode('content/myappname/newnode');
## The admin session is meant to be used in bundles for providing general services, it must be sparingly used and only when absolutely necessary. It's drawbacks are well documented on the internet and there are alternatives. When possible it is always best to get session from request which will have user specific priviliges