Expand my Community achievements bar.

SOLVED

Issue displaying a property in a JSP under jcr:content

Avatar

Level 1

Hi all,

Having those cq5 trials and tribulations over the last couple of days and would appreciated some help in regards to a problem I have. I must say I'm pretty new to CQ5 but have read through the documentation. I am using version 5.2.

 

Requirement:

Content editors can edit a field of text from the WCM.

Page is rendered as json, with no HTML markup displaying that piece of text.

Currently, I have it set up so I have a standard page setup editable by the WCM, with a seperate JSP (json.jsp) in the component (that the page is based on) which is called by:

/content/test/page.json.html

This is fine when displaying children of the current content. I can get the current node, it's children and some properties. However, I cannot see things under a par-sys element in the jcr:content for that page. Another thing making this a bit tricky is that when I load this page in the WCM (the JSON page), I get a loading image in the tools menu, so cannot customise content, therefore having to use a main page to edit, and a seperate JSON view to render the content as JSON. When I edit in the main page with a component (extended from par-base) inside a par-sys (called main-stage), my content displays on this page, but when trying to display in the JSON view it cannot see this data.

 

Doing things like:

 

    Set<String> keys = (Set <String>) currentPage.getProperties().keySet ();
    out.write ("main-stage" + properties + "\n\n");
    for (String key : keys) {
        out.write("key: " + key + "\n");
    }

I cannot see the jcr:content, just the following:

 

key: sling:resourceType
key: jcr:uuid
key: jcr:mixinTypes
key: jcr:title
key: cq:parentPath
key: cq:lastReplicationAction
key: jcr:baseVersion
key: jcr:isCheckedOut
key: cq:lastModifiedBy
key: cq:template
key: jcr:primaryType
key: jcr:predecessors
key: jcr:versionHistory
key: cq:lastReplicatedBy
key: cq:name
key: cq:lastModified
key: cq:childrenOrder
key: cq:lastReplicated
key: cq:siblingOrder
path: /content/test/glossary/g/test5
cq:siblingOrder: null

properties.get below:
main-stage: null
main-stage/term: null
main-stage/text: null
main-stage/text/text: null
main-stage/text/text: null

 

and:

<cq:include path="main-stage/text" resourceType="/apps/test/components/term" />

Renders the item, but it cannot find the data so returns the default text back.

I appreciate I may not be doing things the correct way, so all feedback would be welcome, but essentially, I'm trying to display a property under jcr:content in a JSP and failing badly.

Thanks in advance,

Luke

1 Accepted Solution

Avatar

Correct answer by
Level 8

I think the primary issue is how you are trying to access the content below jcr:content. You can only use properties.get for properties of the current node. The object currentProperties is a ValueMap for jcr:content. You can not us ValueMap.getProperties to access the properties of child nodes (like main-stage or it's children). 

In addition I think you may need consider that you can't predict what the child nodes of the paragraph system will be named. As components are added to the paragraph system it names to nodes using a combination of the component name and sequence number. This means you can't hard code the node names and you have to iterate over the children. 

So you code would need to look more like:

Set<String> keys = (Set <String>) currentPage.getProperties().keySet (); out.write ("jcr:cotent properties\n\n"); for (String key : keys) { out.write("key: " + key + "\n"); } Resource parsysResource = resource.getChild("main-stage"); Iterator<Resource> childResources = parsysResource.listChildren(); out.write("main-stage properties\n\n"); while (childResources.hasNext()) { Resource childResource = childResources.next(); ValueMap childProperties = childResource.adaptTo(ValueMap.class); Set<String> childkeys = (Set <String>) childProperties.keySet (); out.write (childResource.getName() + " properties\n\n"); for (String childkey : childkeys) { out.write("key: " + childkey + "\n"); } }

That code isn't tested, and honestly I would never use out.write in production code in a JSP, but it gives the idea of how the API would work. 

View solution in original post

1 Reply

Avatar

Correct answer by
Level 8

I think the primary issue is how you are trying to access the content below jcr:content. You can only use properties.get for properties of the current node. The object currentProperties is a ValueMap for jcr:content. You can not us ValueMap.getProperties to access the properties of child nodes (like main-stage or it's children). 

In addition I think you may need consider that you can't predict what the child nodes of the paragraph system will be named. As components are added to the paragraph system it names to nodes using a combination of the component name and sequence number. This means you can't hard code the node names and you have to iterate over the children. 

So you code would need to look more like:

Set<String> keys = (Set <String>) currentPage.getProperties().keySet (); out.write ("jcr:cotent properties\n\n"); for (String key : keys) { out.write("key: " + key + "\n"); } Resource parsysResource = resource.getChild("main-stage"); Iterator<Resource> childResources = parsysResource.listChildren(); out.write("main-stage properties\n\n"); while (childResources.hasNext()) { Resource childResource = childResources.next(); ValueMap childProperties = childResource.adaptTo(ValueMap.class); Set<String> childkeys = (Set <String>) childProperties.keySet (); out.write (childResource.getName() + " properties\n\n"); for (String childkey : childkeys) { out.write("key: " + childkey + "\n"); } }

That code isn't tested, and honestly I would never use out.write in production code in a JSP, but it gives the idea of how the API would work.