Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker - access static variables of an object

Tags:

freemarker

I have a simple class:

public class MyClass {

     public final static long MAIN = 1;

     @Setter @Getter
     protected int id;
}

(@Setter @Getter are lombok annotations for Setter and Getter methods.)

In Freemarker template I would like to create a condition like:

<#if myClassInstance.id == myClassInstance.MAIN>

But the right hand side of the if expression is according to FreeMarker undefined. Is there a way to do this? Thanks!

like image 521
Vojtěch Avatar asked Mar 11 '12 22:03

Vojtěch


People also ask

Does FreeMarker have content?

The has_content FunctionFreeMarker has built-in functions to detect for variables. The most common method of detecting for empty or null values uses the has_content function and the trim function. The has_content function is often used with another built-in FreeMarker function to detect for null values.

How do I print a value in FreeMarker?

For example, if you create a variable called "foo" in the template, you can print its value with ${foo} . If, coincidently, there's a variable called "foo" in the data-model too, the variable created in the template will hide (not overwrite!)


1 Answers

The template language is not aware of Java classes. But you can expose static members through the data-model (template context). See: http://freemarker.org/docs/pgui_misc_beanwrapper.html#autoid_55

like image 148
ddekany Avatar answered Oct 11 '22 21:10

ddekany