Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

This article is an extension to Discover Sling in 15 minutes  showing how to use sling.include in JSP . Its best to look at Discover Sling in 15 minutes  before continuing this article.


sling.include can be called in two ways in a JSP script.

  • using <sling:include> tag.
  • Wiki Markup
    using <% sling.include("\[path to resource\]") ; %>&nbsp;

To use sling:include in a JSP script , you have to use <sling:defineObjects/>  tag.

 The examples in Discover Sling in 15 minutes for using sling.include should be changed to jsp as follows,

The header.jsp is as follows, 

Code Block
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>

<sling:defineObjects/>

<div>
	<p style="color:blue;">
		<img src="/images/sling.jpg"  align="right"/>
			<%= currentNode.getProperty("headline").getValue().getString() %>
	</p>
</div>

The html.jsp can be written in one of the following ways,

Wiki Markup
If you are using <% sling.include("\[path to resource\]") ; %>&nbsp; create html.jsp as follows,

Code Block
<%@page session="false"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>

<sling:defineObjects/>

<html>
	<body>
		<div id="header">
			<% sling.include("/content/header"); %>
		</div>
		<h1><%= currentNode.getProperty("title").getValue().getString() %></h1>
	</body>
</html>

If you are using <sling:include> tag create html.jsp as follows,

Code Block
<%@page session="false"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>

<sling:defineObjects/>

<html>
	<body>
		<div id="header">
			<sling:include path="/content/header" />
		</div>
		<h1><%= currentNode.getProperty("title").getValue().getString() %></h1>
	</body>
</html>

References:

For more information on other scripting variables look at Common scripting variables page.