Passing a List of Objects to Freemarker and then Looping
Date : March 29 2020, 07:55 AM
I wish this helpful for you Is "jobs" really a collection? Please post a snippet of code where you are creating and processing your template. I just wrote a quick test to check: public void testFreeMarker() throws Exception {
List<Invoice> invoices = Arrays.asList(
new Invoice( "note1", "amount1" ),
new Invoice( "note2", "amount2" ) );
Map<String, Object> root = new HashMap<String, Object>();
root.put( "invoices", invoices );
StringWriter out = new StringWriter();
Configuration cfg = new Configuration();
cfg.setClassForTemplateLoading( FreemarkerUtils.class, "/templates" );
cfg.setObjectWrapper( new DefaultObjectWrapper() );
Template temp = cfg.getTemplate( "listTest.ftl" );
temp.process( root, out );
System.out.println( out.getBuffer().toString() );
}
<#list invoices as invoice>
Item: ${invoice.note} - ${invoice.amount}
</#list>
Item: note1 - amount1
Item: note2 - amount2
|
freemarker error: expected hash. evaluated instead to freemarker.template.SimpleScalar
Date : March 29 2020, 07:55 AM
Does that help With <#assign senti = "${scmr.results[model]}"> you have converted scmr.results[model] to a String (a scalar), that's why. Just write <#assign senti = scmr.results[model]>. In FreeMarker expressions you can inject value into a string literal, like "Hello ${name}!" (same as "Hello " + name + "!"), and "${someExpression}" is just a case of that. It's not like in JSP.
|
Freemarker how to access next element in sequence
Tag : java , By : user170635
Date : March 29 2020, 07:55 AM
wish help you to fix your issue You try to get an item out of element, but element is already the item out of elements. In this case, elements has to be a sequence, from which you can get items, so your code should look like <#list elements as element>
<#if element_index < elements?size-1>
${elements[element_index + 1]}
</#if>
</#list>
|
FreeMarker Error : left-hand operand: Expected a hash, but this evaluated to a sequence
Date : March 29 2020, 07:55 AM
may help you . I Hope you've accessing it wrongly. As per your example, the list name is product. So, <#assign totalProducts = cModel.getProducts()?size>
|
freemarker sequence index calculation
Date : March 29 2020, 07:55 AM
Hope that helps As of the error you get, when you reach the index of the last item, currIndex + 1 will point after the last item, hence, that item is missing. Anyway, here's the fixed version with some prettifying. <#assign dupMessageIds = []>
<#list arrMsdIds as itemId>
<#if itemId?has_next && arrMsdIds[itemId?index + 1] == itemId>
<#assign dupMessageIds += [itemId] />
</#if>
</#list>
|