cancel
Showing results for 
Search instead for 
Did you mean: 

How to foreach an array returned from a Java class in a html webscript?

mbel
Star Contributor
Star Contributor

Hello,

I have created a webscript and Java class which return some data.

The data is a JSONObject {"data": [{.....}]) which present an array. So, I want somehow to foreach the data in the html.

1st how I can use this JSONObject which comes from Java in a javascript file and after that to foreach it in some <li> element

2nd If I send from the Java class directly an Array, again how to loop it in the html.

I tried using some Freemarker expressions as below:

<#list data>

   <ul>

     <#items as item>

       <li>${item.name}</li>

     </#items>

   </ul>

  <#else>

   <p>Empty</p>

  </#list>

But I received the below exception:

freemarker.core.ParseException - Parsing error in template "xxx.post.html.ftl" in line 10, column 22: Encountered ">", but was expecting one of: "as" "." "[" "(" "?" "!" <TERMINATING_EXCLAM> "??" "+" "-" "*" "/" "%" "!=" "=" "==" ">=" <ESCAPED_GTE> ">" <ESCAPED_GT> <LESS_THAN_EQUALS> <LESS_THAN> ".." <AND> <OR>

So obviously I haven't understood the syntax...

Could someone assists in both cases when I have an array and when I have JsonObject to use in js file.

Thank you in advance.

1 ACCEPTED ANSWER

kaynezhang
World-Class Innovator
World-Class Innovator

Axel Faust​ Missed character / in the end  </#if> tag

<#if items??>

   <ul>

     <#list items as item>

       <li>${item.name}</li>

     </#list>

   </ul>

  <#else>

   <p>Empty</p>

</#if>

View answer in original post

10 REPLIES 10

afaust
Legendary Innovator
Legendary Innovator

The syntax should be:

<#if items??>

   <ul>

     <#list items as item>

       <li>${item.name}</li>

     </#list>

   </ul>

  <#else>

   <p>Empty</p>

  <#if>

One additional thing you have to keep in mind is that a JSONObject or arbitrary Java object may not be iterable in FreeMarker. The best types for handling sequences in FTL are Collection or array types.

mbel
Star Contributor
Star Contributor

I changed to your syntax and now I have the below error.

Caused by: freemarker.core.ParseException:

Unknown directive: #if. Help (latest version): http://freemarker.org/docs/ref_directive_alphaidx.html; you're using FreeMarker 2.3.20.

Just to mention that I have generated maven alfresco amp archetype, so I havent made any custom changes concerning FreeMarker.

Could you please tell me also how I can use the returned object from java in webscript js file?

Because if I am not able to use the FreeMarker, I can foreach the data in the js.

mbel
Star Contributor
Star Contributor

The above exception was because in your syntax the last <#if> should be a closing tag.

However, Could you please answer me to the above question regarding the js file?

Thank you in advance.

kaynezhang
World-Class Innovator
World-Class Innovator

where would you like to run your javascript in ?

If it is in server side ,you already use java backed webscript,you don't need javascript.

If it is running in share you can do it like this

     result = remote.call("/you webscript url");

      var items = JSON.parse(result),

      for (var i = 0, ii = items.length; i < ii; i++)

      {

            var item = items[i];

       }

mbel
Star Contributor
Star Contributor

Could you please provide documentation about remote api?

I receive error message that remote is not defined:

Uncaught ReferenceError: remote is not defined

afaust
Legendary Innovator
Legendary Innovator

Documentation can be found in docs.alfresco.com for Surf and Repository-tier. The fact that "remote" is not defined means that your web script is run on Repository-tier (not in Share) which does not provide this object.

mbel
Star Contributor
Star Contributor

Yes, its not in Share.

mbel
Star Contributor
Star Contributor

instead of remote.call method, can I use ${JsonObjectFromJava} somehow.

Something like that -> var items = JSON.parse(${JsonObjectFromJava})?

kaynezhang
World-Class Innovator
World-Class Innovator

Try jsonUtils.toJSONObject(JsonObjectFromJava)