java 在 JSP 中,如何识别 List 中存在的 Object 类型?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1072772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 15:03:26  来源:igfitidea点击:

In JSP, how to identify the type of Object present in a List?

javajspjstlscriptlet

提问by Rakesh Juyal

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

在JSP中是否可以像我们在Java中那样获取List中的Object类型

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.

我不喜欢 scriptlets,但如果没有 scriptlets 是不可能的,那么请让我知道甚至那个解决方案。

  • assuming all objects in list are of same type.
  • 假设列表中的所有对象都是相同类型的。

回答by Ben

Using JSTL, you can retrieve everything that uses the JavaBean spec - if you want to use getClass() in java, you would use .class in JSTL:

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

This would write out your classname:

这将写出您的类名:

${myList[0].class}

回答by superaarthi

I realize this question is 6 years old; however if anyone searching for how to get the Java class of an Object in JSP finds this question, note that current versions of JSP actually do not allow this notation. You would have to do

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

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

instead. If you want the class name as a string, this method works well with the .name method mentioned above. You would do

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

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

You can find out more here: https://bz.apache.org/bugzilla/show_bug.cgi?id=50120

您可以在此处了解更多信息:https: //bz.apache.org/bugzilla/show_bug.cgi?id=50120

Hope this helps someone!

希望这对某人有帮助!