Creating a nested custom tag using jsp2 tag files

I want to create custom jsp tags. Following is my requirement.

1. The tag that I have to create can always be added as a chlild tag for specific tags. So I want to check the tag to see if it is in a valid parent tag.

2. I want to access the attributes of the parent tag in the child tag file and vice versa.

3.I also want to set one property for each tag, which can be set from the tag file, and the user cannot set it.

I would like to know if I can execute them with tag files or do I need to create custom tags using java code? If this can be done in suing tag files, can you give an example?

+6
source share
1 answer

Navigating the tag tree, which is required for verification at your first point, is possible only inside the custom tag (implementing SimpleTag and using its getParent method), but not in the user tag file.

Example of checking the type of parent and setting the attribute:

 JspTag jspTag = getParent(); if (jspTag instance MyCustomTag) { MyCustomTag myCustomTag = (MyCustomTag) jspTag; myCustomTag.setFoo("bar"); } 
0
source

Source: https://habr.com/ru/post/905301/


All Articles