开发者

velocity (test instanceof)

开发者 https://www.devze.com 2023-01-04 10:18 出处:网络
How can I test the class of a given object in a velocity template. I can\'t f开发者_运维知识库ind an instanceof directiveThere is no instanceof, but you can get class name as a string and then go from

How can I test the class of a given object in a velocity template. I can't f开发者_运维知识库ind an instanceof directive


There is no instanceof, but you can get class name as a string and then go from there:

${myObj.class.name} would return "com.test.MyObj"
${myObj.class.simpleName} would return "MyObj"


In Java you can set this variable

    [VelocityValuesMap].put("TestClass", TestClass.class);
    [VelocityValuesMap].put("myObject", myObject);

and inside the velocity template you can do this

($TestClass.isAssignableFrom($myObj.getClass()))

0

精彩评论

暂无评论...
验证码 换一张
取 消