FreeMarker检查对象的类名 [英] FreeMarker check the class name of an object

查看:89
本文介绍了FreeMarker检查对象的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆