开发者

Is there a way to restrict usage of a custom tag so that it can only be used within a specific tag?

开发者 https://www.devze.com 2023-04-01 17:13 出处:网络
I have a <filter:filters> tag and a <filter:filter> tag. These are JSP custom tags. Is there any way to specify that开发者_运维技巧 a <filter:filter> tag cannot be used in isolation

I have a <filter:filters> tag and a <filter:filter> tag. These are JSP custom tags. Is there any way to specify that开发者_运维技巧 a <filter:filter> tag cannot be used in isolation and that it can only be used as a child of the <filter:filters> tag?


There doesn't appear to be a declarative/TLD way to do this.

JSP 2.0 allows you to implement a translation-time translator by extending the class javax.servlet.jsp.tagext.TagLibraryValidator and implementing the validate message.

You could also do it at runtime through the use of the SimpleTag.getParent() method and throw an exception if the parent is not the correct type.

It might look something like this:

public void doTag() {
   if (!(getParent() instanceof Filters)) 
      throw new Exception("A filter can only be used inside a filters tag.");
}
0

精彩评论

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

关注公众号