8

Is it possible in JSP to get the type of Object in List, just like we do in Java

myDataBind.getResultsList().get(0).getClass();

or is it possible to achieve something like this:

if ( myDataBind.getResultsList().get(0) instanceOf MyClass ) {
  doThis;
}

i don't prefer scriptlets, but if it is not possible to do without scriptlets then Please let me know even that solution too.

  • assuming all objects in list are of same type.
4

2 回答 2

13

使用 JSTL,您可以检索使用 JavaBean 规范的所有内容 - 如果您想在 java 中使用 getClass(),您可以在 JSTL 中使用 .class:

这会写出你的类名:

${myList[0].class}
于 2009-07-02T06:30:14.537 回答
12

我意识到这个问题已经 6 岁了;但是,如果有人在搜索如何在 JSP 中获取 Object 的 Java 类时发现了这个问题,请注意当前版本的 JSP 实际上不允许这种表示法。你必须做

${myList[0]['class']}

反而。如果你想把类名作为一个字符串,这个方法和上面提到的 .name 方法配合得很好。你会做

${myList[0]['class'].name}

您可以在这里找到更多信息:https ://bz.apache.org/bugzilla/show_bug.cgi?id=50120

希望这对某人有帮助!

于 2015-08-11T20:56:22.500 回答