问题
is there a way to get the class name of an object in a freemarker template ?
For instance:
<#if component.javaType.class.name.equals("test") >
"something...."
</#else>
"something else ...."
</#if>
Thanks
回答1:
There's no feature built in for that, but depending on the configuration settings and on the type of the object, this may works:
<#if component.class.name == 'com.example.Something'>
That works because component.foo simply means comonent.getFoo() in Java, so the above just means component.getClass().getName(). This, however doesn't work if the JavaBean properties of component aren't exposed, which (assuming the usual FreeMarker configuration) is the case for String-s, Number-s, Map-s, List-s and some more "standard" classes. If component can be a such object, but the comparison should be false for them anyway, you can write (component.class.name)!'unknown' == 'com.example.Something'.
来源:https://stackoverflow.com/questions/41572548/freemarker-check-the-class-name-of-an-object