FreeMarker check the class name of an object

不羁岁月 提交于 2020-01-15 11:22:18

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!