Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Passing Javascript array to Java Use API

Avatar

Level 5

Following up on this discussion: http://help-forums.adobe.com/content/adobeforums/en/experience-manager-forum/adobe-experience-manage...

Now that I have an array of different parameters, I'd like to pass them to a Java file where im implementing the Use-API. I've dont some digging and have found that when I try to pass the object returned by my JS file, that is read as a io.sightly.js.rhino.HybridObject.

I can find no documentation for this type aside from SVN commits (which actually refer to org.apache.sling.scripting.sightly.js.rhino) to the sling project over the last 6 months or so, nor can I figure out how to include this in my Maven configuration.

Is there a way to implement something like this now, or do i need to wait for HybridObject to be accepted in the sling project?

1 Accepted Solution

Avatar

Correct answer by
Employee

I got this working with the following code..., have a look if this is also ok for you:

JAVA: import io.sightly.java.api.Record; import com.adobe.cq.sightly.WCMUse; public class UseTest extends WCMUse { public Record params=null; public String p; @Override public void activate() throws Exception { params = get("params", Record.class); p = (String) params.get("dateRange"); } }

Sightly:

<div data-sly-use.newsList="newsList.js">
${newsList.dateRange}
        <div data-sly-use.articles="${'yourpackage.UseTest' @ params=newsList}">
            ${articles.p}
        </div>
</div>

View solution in original post

4 Replies

Avatar

Employee

Are you able to share a basic code sample what you are trying to do?

Perhaps I can help you.

Avatar

Level 5

Sure

...(edited from my original post to be slightly more clear)

Sightly HTML

<div data-sly-use.newsList="newsList.js">         ${newsList.test}         <div data-sly-use.test="${'com.uc.news.UseTest' @ params = newsList}">             test output: ${test.p}         </div> </div>

newsList.js:

"use strict"; use( function () { var params = {}; params.limit = 0; params.page = 0; params.term = ""; params.articles = ""; params.articlesExclude = ""; params.alsoQuery = "false"; params.author = ""; params.authorsExclude = ""; params.contacts = ""; params.dateRange = "unlimited"; params.startDate = "0"; params.endDate = "TODAY"; params.hasVideo = false; params.tags = []; params.anyall = "any";         params.test = "success"; return params; });

UseTest.java

package com.uc.news; import com.adobe.cq.sightly.WCMUse; public class UseTest extends WCMUse { String p = ""; @Override public void activate() throws Exception { String p = (String) get("params", String.class); //Throws Error: } }

HTML ouput:

<div> success <div> test output: </div> </div>

Error from log: 

04.04.2015 15:54:53.594 *ERROR* [0:0:0:0:0:0:0:1 [1428177293144] GET /content/news/search-results.html HTTP/1.1] com.adobe.cq.sightly.WCMUse Failed to cast value java.lang.ClassCastException: Cannot cast io.sightly.js.rhino.HybridObject to java.lang.String at java.lang.Class.cast(Class.java:3361) at com.adobe.cq.sightly.WCMUse.get(WCMUse.java:87) at com.uc.news.UseTest.activate(UseTest.java:11)

Avatar

Correct answer by
Employee

I got this working with the following code..., have a look if this is also ok for you:

JAVA: import io.sightly.java.api.Record; import com.adobe.cq.sightly.WCMUse; public class UseTest extends WCMUse { public Record params=null; public String p; @Override public void activate() throws Exception { params = get("params", Record.class); p = (String) params.get("dateRange"); } }

Sightly:

<div data-sly-use.newsList="newsList.js">
${newsList.dateRange}
        <div data-sly-use.articles="${'yourpackage.UseTest' @ params=newsList}">
            ${articles.p}
        </div>
</div>