FreeMarker检查对象的类名 [英] FreeMarker check the class name of an object
问题描述
有没有办法在freemarker模板中获取对象的类名?
is there a way to get the class name of an object in a freemarker template ?
例如:
<#if component.javaType.class.name.equals("test") >
"something...."
</#else>
"something else ...."
</#if>
谢谢
推荐答案
没有为此内置的功能,但是根据配置设置和对象的类型,这可能有效:
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'>
之所以有用,是因为component.foo
在Java中仅表示comonent.getFoo()
,因此以上仅表示component.getClass().getName()
.但是,如果未公开component
的JavaBean属性,这是行不通的(假设使用常规的FreeMarker配置),String
-s,Number
-s,Map
-s就是这种情况, List
-s和其他一些标准"类.如果component
可以是这样的对象,但是无论如何它们的比较都应该为假,则可以编写(component.class.name)!'unknown' == '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'
.
这篇关于FreeMarker检查对象的类名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!