Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I call java methods on an object from a FreeMarker template?

Is it possible to call a method that takes parameters from a Freemarker template?

I have an object model that I'm trying to render with Freemarker into a web page. One of the objects has a method to get a sublist of it's contents - taking a parameter that is used to filter the list:

public List getunits(final String type);

I know in JSP you can't do this directly, but you can write custom functions that will allow you to achieve the result you want. How do you solve this in Freemarker? Is it the same with writing custom functions? Or is there some way of actually calling this kind of function?

like image 307
Graham Avatar asked Sep 01 '09 12:09

Graham


People also ask

What is the use of FreeMarker template?

FreeMarker is a template engine, written in Java, and maintained by the Apache Foundation. We can use the FreeMarker Template Language, also known as FTL, to generate many text-based formats like web pages, email, or XML files.

How do you comment out code on FreeMarker?

Comments: <#-- and --> Comments are similar to HTML comments, but they are delimited by <#-- and -->. Comments will be ignored by FreeMarker, and will not be written to the output.

What is eval in FreeMarker?

eval. This built-in evaluates a string as an FTL expression. For example "1+2"? eval returns the number 3. (To render a template that's stored in a string, use the interpret built-in instead.)

What is FreeMarker Template Error?

Description. Description: FreeMarker template error appears in logs when attempting to add a Web Content Article to a page or search for it in the Search portlet. The issue only occurs when the Summary field is empty and when using the following line in the template.


1 Answers

FreeMarker allows invoking methods that were made available through the model from within expressions.

Assuming your object has been exposed as myBean you can invoke the method as follows:

<#list myBean.getunits("myType") as unit>   do stuff with ${unit} </#list> 

You don't have to use <list>, of course, it's just there as an example since your method returns a list.

like image 141
ChssPly76 Avatar answered Sep 19 '22 15:09

ChssPly76