incubator-sling-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Sling Website > Discover Sling in 15 minutes
Date Fri, 13 Nov 2009 14:30:00 GMT
<html>
<head>
    <base href="http://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/1519/1/1/_/styles/combined.css?spaceKey=SLINGxSITE&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background-color: white" bgcolor="white">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
     <h2><a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Discover+Sling+in+15+minutes">Discover
Sling in 15 minutes</a></h2>
     <h4>Page <b>edited</b> by             <a href="http://cwiki.apache.org/confluence/display/~cziegeler@apache.org">Carsten
Ziegeler</a>
    </h4>
     
          <br/>
     <div class="notificationGreySide">
         <h1><a name="DiscoverSlingin15minutes-DiscoverSlingin15minutestheSlingLaunchpad"></a>Discover
Sling in 15 minutes - the Sling Launchpad</h1>

<p>The Sling Launchpad is a ready-to-run Sling configuration, providing an embedded
JCR content repository and web server, a selection of Sling components, and documentation
and examples. The Launchpad makes it easy to get started with Sling and to develop script-based
applications.</p>

<p>This page will help you in getting started with the Launchpad. Fifteen minutes should
be enough to get an overview of what Sling does.</p>

<p>While simple to run and understand, the Launchpad is a full-featured instance of
Sling, an example configuration that we have created with the most common modules and configurations.
The full functionality of Sling is available by loading additional Sling (or custom) OSGi
modules as needed, using the Launchpad's web-based OSGi management console.</p>

<h2><a name="DiscoverSlingin15minutes-SeeAlso"></a>See Also</h2>

<p>Example applications and mini-applications for Sling can be found under <a href="http://svn.apache.org/repos/asf/sling/trunk/samples/"
rel="nofollow">http://svn.apache.org/repos/asf/sling/trunk/samples/</a> . Once you
grok the basic examples of this page, we recommend studying the <em>espblog</em>
and <em>webloader</em> samples for more complete examples. The <em>javashell</em>
sample is useful to play with JCR java code (or any java code, for that matter) interactively.</p>

<h2><a name="DiscoverSlingin15minutes-Prerequisites"></a>Prerequisites</h2>

<p>If using the self-runnable jar from the Sling distribution, you only need a Java
5 JDK.</p>

<p>If using the war file from the Sling distribution, you need a suitable servlet container
(all recent versions of Jetty or Tomcat should work), running under a Java 5 JDK.</p>

<p>If building Sling yourself (which is the best way to get the latest and greatest),
you'll need:</p>
<ul>
	<li>A Subversion client to get the Sling code.</li>
	<li>A Java 5 JDK.</li>
	<li><a href="http://maven.apache.org/" rel="nofollow">Maven</a> for the
build, we currently recommend V 2.0.7 (and see our <a href="/confluence/display/SLINGxSITE/MavenTipsAndTricks"
title="MavenTipsAndTricks">MavenTipsAndTricks</a>).</li>
</ul>


<p>And in all cases you'll need <a href="http://curl.haxx.se/" rel="nofollow">cURL</a>
to run the examples below. Any HTTP client would do, but cURL is the easiest to document in
a reproducible way.</p>

<p>A WebDAV client makes editing server-side scripts much more convenient, but to make
our examples easy to reproduce, we're using cURL below to create and update files in the JCR
repository, via the Sling WebDAV server.</p>

<h2><a name="DiscoverSlingin15minutes-GettheLaunchpad"></a>Get the Launchpad</h2>

<p>See <a href="/confluence/display/SLINGxSITE/Getting+and+Building+Sling" title="Getting
and Building Sling">Getting and Building Sling</a> &#45; you can either use a
released version (if it is current enough), or build it yourself.</p>

<h2><a name="DiscoverSlingin15minutes-StarttheLaunchpad"></a>Start the Launchpad</h2>

<p>If using the self-runnable jar from the Sling distribution, start it by double-clicking
or with <em>java &#45;jar ...</em>.</p>

<p>If using the war file from the Sling distribution, install it in your servlet container
and start that.</p>

<p>If you built Sling yourself, change to the <em>launchpad/webapp</em>
directory under the top-level <em>sling</em> directory, and run</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>mvn jetty:run
</pre>
</div></div>
<p>To start the launchpad.</p>

<p>The examples below assume that Sling is running on port 8080, which is the default
for the <em>launchpad/webapp</em> module. If your setup is different you'll need
to adjust the port number accordingly.</p>

<p>Once started, look at <a href="http://localhost:8080/system/console/bundles" rel="nofollow">http://localhost:8080/system/console/bundles</a>
with your browser. Use <em>admin</em> with password <em>admin</em>
if Sling asks you for a login. Sling then displays the <em>Felix Web Management Console</em>
page.</p>


<p>On the bundles page, all bundles should be marked <em>Active</em>. They're
all <a href="http://www.osgi.org/" rel="nofollow">OSGi</a> bundles powered by
<a href="http://felix.apache.org" rel="nofollow">Apache Felix</a>, but that doesn't
really matter to us right now.</p>
<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Log files</b><br
/><p>If things go wrong, have a look at the <tt>target/sling/logs/error.log</tt>
log file under <tt>launchpad/webapp</tt> &#45; that's where Sling writes any
error messages.</p></td></tr></table></div>

<h2><a name="DiscoverSlingin15minutes-Createsomecontent"></a>Create some
content</h2>

<p>Until we have ready-to-test forms, you can create content with cURL, or you can create
an HTML form that posts to the specified URL.</p>

<p>To create a content node (nodes are a <a href="http://jackrabbit.apache.org/"
rel="nofollow">JCR</a> concept, a unit of storage) with cURL, use:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -F"sling:resourceType=foo/bar" -F"title=some title" http://admin:admin@localhost:8080/content/mynode
</pre>
</div></div>
<p>The resulting node can be seen at <a href="http://localhost:8080/content/mynode.html"
rel="nofollow">http://localhost:8080/content/mynode.html</a>, or as json format under
<a href="http://localhost:8080/content/mynode.json" rel="nofollow">http://localhost:8080/content/mynode.json</a>.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Monitoring requests</b><br
/><p>Sling provides a simple tool (an OSGi console plugin) to monitor HTTP requests,
which helps understand how things work internally. See the <a href="/confluence/display/SLINGxSITE/Monitoring+Requests"
title="Monitoring Requests">Monitoring Requests</a> page for details.</p></td></tr></table></div>

<h2><a name="DiscoverSlingin15minutes-Renderyourcontentusingserversidejavascript%28ESP%29"></a>Render
your content using server-side javascript (ESP)</h2>

<p>Sling uses scripts or servlets to render and process content.</p>

<p>Several scripting languages are available as additional Sling modules (packaged as
OSGi <em>bundles</em> that can be installed via the Sling management console),
but the launchpad currently only includes the ESP (server-side ECMAscript) and JSP (Java Server
Pages) languages modules by default.</p>

<p>To select a script, Sling uses the node's <em>sling:resourceType</em>
property, if it is set.</p>

<p>That's the case in our example, so the following script will be used by Sling to
render the node in HTML, if the script is found at <em>/apps/foo/bar/html.esp</em>
in the repository.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>html.esp</b></div><div class="codeContent
panelContent">
<pre class="code-java">
&lt;html&gt;   &lt;body&gt;     &lt;h1&gt;&lt;%= currentNode.title
%&gt;&lt;/h1&gt;   &lt;/body&gt; &lt;/html&gt;
</pre>
</div></div>
<p>To select the script, Sling looks under <em>/apps</em>, appends the <em>sling:resourceType</em>
value of our node, which is <em>foo/bar</em>, and appends <em>html.esp</em>
as the extension if our URL is <em>html</em> and the language of our script is
<em>esp</em>.</p>

<p>Store this script under <em>apps/foo/bar/html.esp</em>, either using
a WebDAV client (connected to <a href="http://admin:admin@localhost:8080/" rel="nofollow">http://admin:admin@localhost:8080/</a>),
or using cURL as shown here, after creating the <em>html.esp</em> script in the
current directory on your system:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X MKCOL http://admin:admin@localhost:8080/apps/foo
curl -X MKCOL http://admin:admin@localhost:8080/apps/foo/bar
</pre>
</div></div>
<p>create a local file <em>html.esp</em> and copy above content.</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -T html.esp http://admin:admin@localhost:8080/apps/foo/bar/html.esp
</pre>
</div></div>

<p>The HTML rendering of your node, at <a href="http://localhost:8080/content/mynode.html"
rel="nofollow">http://localhost:8080/content/mynode.html</a>, is now created by this
ESP script. You should see the node's title alone as an &lt;h1&gt; element in that
page.</p>

<p>A script named <em>POST.esp</em> instead of <em>html.esp</em>
would be called for a POST request, <em>DELETE.esp</em> for DELETE, <em>xml.esp</em>
for a GET request with a <em>.xml</em> extension, etc. See <a href="http://cwiki.apache.org/SLING/url-to-script-resolution.html"
rel="nofollow">URL to Script Resolution</a> on the Sling wiki for more info.</p>

<p>Servlets can also be easily "wired" to handle specific resource types, extensions,
etc., in the simplest case by using SCR annotations in the servlet source code. Servlets and
scripts are interchangeable when it comes to processing Sling requests.</p>

<h2><a name="DiscoverSlingin15minutes-Whatnext%3F"></a>What next?</h2>

<p>These simple examples show how Sling uses scripts to work with JCR data, based on
<em>sling:resourceType</em> or node types.</p>

<p>There's much more to Sling of course - you'll find some additional simple examples
below, and above in the <em>see also</em> section.</p>

<p>We are working on debugging features to help trace the way Sling processes requests.
Have a look at <a href="https://issues.apache.org/jira/browse/SLING-3" rel="nofollow">SLING-3</a>
to see what's possible already.</p>

<h1><a name="DiscoverSlingin15minutes-Additionalexamples"></a>Additional
examples</h1>

<h2><a name="DiscoverSlingin15minutes-LetSlinggeneratethepathofanewlycreatednode."></a>Let
Sling generate the path of a newly created node.</h2>

<p>To create a node with a unique path at a given location, end the URL of the POST
request with <em>/&#42;</em>.</p>

<p>In this case, the Sling response redirects to the URL of the created node.</p>

<p>Start by creating a new <em>/blog</em> folder:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X POST "http://admin:admin@localhost:8080/content/blog"
</pre>
</div></div>
<p>And create a node with a Sling-generated name under it:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -D - -F"title=Adventures with Sling" "http://admin:admin@localhost:8080/content/blog/*"
</pre>
</div></div>
<p>Using cURL's <em>&#45;D</em> option shows the full HTTP response,
which includes a <em>Location</em> header to indicate where the new node was created:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>Location: http://localhost:8080/content/blog/adventures_with_slin_0
</pre>
</div></div>
<p>The actual node name might not be <em>adventures_with_slin_0</em> &#45;
depending on existing content in your repository, Sling will find a unique name for this new
node, based on several well-know property values like title, description, etc. which are used
for this if provided.</p>

<p>So, in our case, our new node can be displayed in HTML via the <a href="http://localhost:8080/content/blog/adventures_with_slin_0.html"
rel="nofollow">http://localhost:8080/content/blog/adventures_with_slin_0.html</a>
URL.</p>

<p>Note that we didn't set a <em>sling:resourceType</em> property on our
node, so if you want to render that node with a script, you'll have to store the script under
<em>/apps/nt/unstructured/html.esp</em>.</p>

<h2><a name="DiscoverSlingin15minutes-Addapageheaderwithsling.include"></a>Add
a page header with sling.include</h2>

<p>The <em>sling.include</em> function can be called from scripts to include
the rendered result of another node.</p>

<p>In this example, we create a node at <em>/content/header</em>, rendered
with a logo using an <em>html.esp</em> script, and use that header at the top
of the <em>html.esp</em> script that we previously created for the <em>foo/bar</em>
resource type.</p>

<p>Start by checking that <a href="http://localhost:8080/content/mynode.html" rel="nofollow">http://localhost:8080/content/mynode.html</a>
is rendered using the <em>html.esp</em> script created above.</p>

<p>Create this script and name it <em>header.esp</em>:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>header.esp</b></div><div class="codeContent
panelContent">
<pre class="code-java">
&lt;div&gt;   &lt;p style=<span class="code-quote">"color:blue;"</span>&gt;
    &lt;img src=<span class="code-quote">"/images/sling.jpg"</span> align=<span
class="code-quote">"right"</span>/&gt;     &lt;%= currentNode.headline %&gt;
  &lt;/p&gt; &lt;/div&gt;
</pre>
</div></div>
<p>Upload it so that it is used to render resources having <em>sling:resourceType=foo/header</em>:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X MKCOL  http://admin:admin@localhost:8080/apps/foo/header/
curl -T header.esp http://admin:admin@localhost:8080/apps/foo/header/html.esp
</pre>
</div></div>
<p>Create the header node:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -F"sling:resourceType=foo/header" -F"headline=Hello, Sling world" http://admin:admin@localhost:8080/content/header
</pre>
</div></div>
<p>Upload the logo that the script uses (using some sling.jpg or other logo in the current
directory):</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -X MKCOL  http://admin:admin@localhost:8080/images/
curl -T sling.jpg  http://admin:admin@localhost:8080/images/sling.jpg
</pre>
</div></div>
<p>And check that the header is rendered with the logo at <a href="http://localhost:8080/content/header.html"
rel="nofollow">http://localhost:8080/content/header.html</a>.</p>

<p>Now, update the html.esp script that we created for our first example above, to include
the header:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeHeader panelHeader"
style="border-bottom-width: 1px;"><b>html.esp</b></div><div class="codeContent
panelContent">
<pre class="code-java">
&lt;html&gt;   &lt;body&gt;     &lt;div id=<span class="code-quote">"header"</span>&gt;
      &lt;% sling.include(<span class="code-quote">"/content/header"</span>);
%&gt;     &lt;/div&gt;     &lt;h1&gt;&lt;%= currentNode.title %&gt;&lt;/h1&gt;
  &lt;/body&gt; &lt;/html&gt;
</pre>
</div></div>
<p>And re-upload it to replace the previous version:</p>
<div class="preformatted panel" style="border-width: 1px;"><div class="preformattedContent
panelContent">
<pre>curl -T html.esp http://admin:admin@localhost:8080/apps/foo/bar/html.esp
</pre>
</div></div>
<p>The <a href="http://localhost:8080/content/mynode.html" rel="nofollow">http://localhost:8080/content/mynode.html</a>,
once refreshed, now shows the blue headline and logo, and that layout applies to any node
created with <em>sling:resourceType=foo/bar</em>.</p>
     </div>
     <div id="commentsSection" class="wiki-content pageSection">
       <div style="float: right;">
            <a href="http://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
       </div>

       <a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Discover+Sling+in+15+minutes">View
Online</a>
       |
       <a href="http://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=76179&revisedVersion=28&originalVersion=27">View
Change</a>
              |
       <a href="http://cwiki.apache.org/confluence/display/SLINGxSITE/Discover+Sling+in+15+minutes?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message