Expand my Community achievements bar.

Get currentNode or currentPage inside JSP

Avatar

Level 2

Hello,

is there a way to get current node or current page inside JSP?

I am playing with the data source and I would like to get all property on the current page.

Thank you so much.

Piece of code:

<%@include file="/libs/granite/ui/global.jsp"%>

<%@ page import="com.adobe.granite.ui.components.ds.DataSource" %>

<%@ page import="com.adobe.granite.ui.components.ds.ValueMapResource" %>

<%@ page import="java.util.HashMap" %>

<%@ page import="org.apache.sling.api.wrappers.ValueMapDecorator" %>

<%@ page import="com.adobe.granite.ui.components.ds.SimpleDataSource" %>

<%@ page import="org.apache.commons.collections.iterators.TransformIterator" %>

<%@ page import="java.util.Map" %>

<%@ page import="java.util.LinkedHashMap" %>

<%@ page import="org.apache.commons.collections.Transformer" %>

<%@ page import="org.apache.sling.api.resource.*" %>

<%

    final Map<String, String> listSelectToRender = new LinkedHashMap<String, String>();

Resource datasource = resource.getChild("datasource");

ResourceResolver resolver = resource.getResourceResolver();

ValueMap dsProperties = ResourceUtil.getValueMap(datasource);

Page myPage = //GET current page;

String genericListPath = dsProperties.get("field", String.class);

String rootPath = myPage.getProperty(genericListPath, String.class);

if(genericListPath != null) {

String[] listToken = genericListPath.split(",");

for(String token: listToken) {

listSelectToRender.put(token, token);

token

}

    DataSource ds = new SimpleDataSource(new TransformIterator(listSelectToRender.keySet().iterator(), new Transformer() {

        public Object transform(Object o) {

            String key = (String) o;

            ValueMap vm = new ValueMapDecorator(new HashMap<String, Object>());

            vm.put("value", listSelectToRender);

            vm.put("text", listSelectToRender.get(key));

            return new ValueMapResource(resolver, new ResourceMetadata(), "nt:unstructured", vm);

        }

    }));

   

    request.setAttribute(DataSource.class.getName(), ds);

}

%>

6 Replies

Avatar

Level 2

Hi.

In that line of code have you tried using the global variable "currentPage"?

Avatar

Level 2

Hello,

Yes I tried to use currentPage as global variable but I got a null.

I am using this jsp inside dialog page property/

Avatar

Level 2

It would be useful to know which version of AEM you are using.

Can you replace <%@include file="/libs/granite/ui/global.jsp"%> with the following?

<%@include file="/libs/foundation/global.jsp"%>

Avatar

Level 2

Thank you.

I am using AEM 6.2.

I will replace it.

Avatar

Level 2

Any chance you found how to do this?