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

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>