开发者

How can I figure out the name of a JSP tag in Java?

开发者 https://www.devze.com 2023-01-06 14:12 出处:网络
Currently I have the following code: Tag tag = getParent(); while(tag != null) { parents.append(\"child of \")

Currently I have the following code:

Tag tag = getParent();

while(tag != null) {
    parents.append("child of ")
           .append(tag.getClass().getName())
           .append("\n");开发者_运维技巧
    tag = tag.getParent();
}

when this runs, I get the following:

com.mystuff.mvc.tag.MyTag 
child of org.apache.taglibs.standard.tag.rt.core.IfTag
child of javax.servlet.jsp.tagext.TagAdapter
child of javax.servlet.jsp.tagext.TagAdapter
child of javax.servlet.jsp.tagext.TagAdapter
child of javax.servlet.jsp.tagext.TagAdapter
child of javax.servlet.jsp.tagext.TagAdapter

MyTag is a Java class and so I can get the name from getName(). Unfortunately neither Tag nor TagAdapter give me methods to find out the name of the actual tag. Class.getName() only returns javax.servlet.jsp.tagext.TagAdapter if the tags are .tag files. I understand that the .tag files eventually get converted to .java files and then compiled into .class files. So if I have a tag called awesome.tag then the corresponding file is awesome_tag.java. I don't understand why this name doesn't show up when I call getName().


You could try:

String name = tag.getClass().getName();
if (tag instanceof TagAdapter) {
  name = ((TagAdapter)tag).getAdaptee().getClass().getName();
}
parents.append("child of ").append(name).append("\n");
0

精彩评论

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