Expand my Community achievements bar.

SOLVED

When/Why my component's 'currentNode' default object is returning null in jsp?

Avatar

Former Community Member

Hi,

I have created a page content component which is explained in the excercise and created the hierarchy like below

- training
    -componenets
        -page

            contentpage (cq:Component)
            contentpage.jsp
                head.jsp
                body.jsp    
                    header.jsp
                    content.jsp
                    footer.jsp

            title (cq:Component)

              title.jsp

I also created a title component under training/componenets/page called 'title' its primary function to get the page title dynamically wherever it calls
and the code title.jsp is given below. This is being called under content.jsp attached its code also below

    <%@include file="/libs/foundation/global.jsp"%>
    <%@page session="false" %>
    <%= properties.get("title",currentPage.getTitle()) %>
        <%
            if(currentNode == null)
            out.println("currentNode is null");
            else
            out.println("currentNode is not null");

    %>

i am pasting below the contentpage.jsp for our discussion

<%@include file="/libs/foundation/global.jsp"%>
<%@page session="false" %>
<html>
<cq:include script="head.jsp" />
<cq:include script="body.jsp" />
<cq:include script="/libs/wcm/core/components/init/init.jsp"/>
Title: <%= currentNode.getProperty("jcr:title").getString() %><br />
Name: <%= currentNode.getName() %><br />
Path: <%= currentNode.getPath() %><br />
Depth: <%= currentNode.getDepth() %><br />
    </html>    

body.jsp

<%@include file="/libs/foundation/global.jsp" %>
<body>
<div class="bg">
<cq:include script="header.jsp"/>
<cq:include script="content.jsp"/>
<cq:include script="footer.jsp"/>
</div>
</body>

header.jsp
<%@include file="/libs/foundation/global.jsp" %>
<div class="header">
<div class="container_16">
<div class="grid_8">
<div> logo </div>
</div>
<div class="grid_8">
<div class="search_area">
<div> userinfo </div>
<div> toptoolbar </div>
<div> search </div>
<div class="clear"></div>
</div>
</div>
<div> <cq:include path="topnav" resourceType="training/components/topnav" /> </div>
</div>
</div>

head.jsp
<%@include file="/libs/foundation/global.jsp"%>
<head>
<title><%= currentPage.getTitle() == null ? currentPage.getName() :currentPage.getTitle() %> </title>
</head>

footer.jsp
<%@include file="/libs/foundation/global.jsp" %>
<div class="footer container_16">
<div class="grid_6">
<div> toolbar </div>
</div>
<div class="clear"></div>
</div>
         

content.jsp 

<%@include file="/libs/foundation/global.jsp" %>
<div class="container_16">
<div class="grid_16">
<div> breadcrumb </div>
<!--div> title </div-->
<cq:include  path="title_node" resourceType="training/components/page/title" />
</div>
<div class="grid_12 body_container">
<div> par </div>
</div>
<div class="grid_4 right_container">
<div> newslist </div>
<div> rightpar </div>
</div>
<div class="clear"></div>
</div>

The project structure is attached in the screenshot for reference.

Question.
1. contentpage.jsp is printing the property using 'currentNode'. But title.jsp is returning null why?
2. Both 'contentpage' and 'title' are of type cq:Component, apart from that i didn't find any difference except that am seeing sling:resourceSuperType = foundation/components/page property is configured for 'contentpage' componenet. Does it make any difference here? 

1 Accepted Solution

Avatar

Correct answer by
Level 8

The answer to your question is that the two components have different current nodes. Each component is typically represented by it's own node in the page's content structure. So a typical page structure you'd have something like this:

  • pagea
    • jcr:content
      • component1
      • component2
      • parsys
        • component3
        • component4

So in your example currentNode in contentpage.jsp represent the jcr:content node or the page node, where as in title.jsp currentNode represents a child node of jcr:content - probably jcr:content/title. It is returning null because the node doesn't exist yet. If a component is included by the template as your title component is the node is only created if an author opens the dialog and saves content to the node. In the case of components being dragged into a paragraph system the node is created when the component is dragged on. 

This oddity is one of the reasons why generally you want to avoid the use of JCR API in component JSPs and stick with the Sling Resource API (resource object or the properties object for almost anything you'd use the currentNode object).  

View solution in original post

1 Reply

Avatar

Correct answer by
Level 8

The answer to your question is that the two components have different current nodes. Each component is typically represented by it's own node in the page's content structure. So a typical page structure you'd have something like this:

  • pagea
    • jcr:content
      • component1
      • component2
      • parsys
        • component3
        • component4

So in your example currentNode in contentpage.jsp represent the jcr:content node or the page node, where as in title.jsp currentNode represents a child node of jcr:content - probably jcr:content/title. It is returning null because the node doesn't exist yet. If a component is included by the template as your title component is the node is only created if an author opens the dialog and saves content to the node. In the case of components being dragged into a paragraph system the node is created when the component is dragged on. 

This oddity is one of the reasons why generally you want to avoid the use of JCR API in component JSPs and stick with the Sling Resource API (resource object or the properties object for almost anything you'd use the currentNode object).