Introduction
This article shows how you can use include in jsp. To use include in jsp and access Sling variables there are two tags available in Sling. If you want to use Sling variables in your script the <sling:defineObjects/> tag should be used.
If you want to use Sling's include feature the you have to give the parameters in the <sling:include .. /> tag.
Before using these tags you have to import the tag library with
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
...
Using path with resourceType to include a script.
Using resource with resourceType to include a script.
Include sample in Discover Sling in 15 minutes using <sling:include .../> jsp tag
...
.
sling.include can be called in two ways in a JSP script.
- using <sling:include> tag.unmigrated-wiki-markup
- using <% sling.include("\[path to resource\]") ; %> %>
To use sling:include in a JSP script , you have to use <sling:defineObjects/> tag.
...
The html.jsp can be written in one of the following ways,
If you are using <% sling.include("\[path to resource\]") ; %> create ; %> create html.jsp as follows, Wiki Markup
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> |
...
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> |
How to use selectors
The above examples will assume all your resources are rendered using a html.jsp at each node. Then you might have a question as to how can I use more than one script to work with more than one resource.
...
this will include the selector.jsp in the html.jsp file.
...
References:
For more information on other scripting variables look at Common scripting variables page.