11-09-2016 03:50 AM
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.
11-09-2016 07:12 AM
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>
11-09-2016 05:24 AM
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.
11-09-2016 06:53 AM
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.
11-09-2016 07:11 AM
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.
11-09-2016 07:31 AM
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];
}
11-09-2016 08:41 AM
Could you please provide documentation about remote api?
I receive error message that remote is not defined:
Uncaught ReferenceError: remote is not defined
11-09-2016 09:07 AM
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.
11-09-2016 10:16 AM
Yes, its not in Share.
11-09-2016 04:35 PM
instead of remote.call method, can I use ${JsonObjectFromJava} somehow.
Something like that -> var items = JSON.parse(${JsonObjectFromJava})?
11-09-2016 10:10 PM
Try jsonUtils.toJSONObject(JsonObjectFromJava)
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.