I spent a good portion of the weekend with my head wrapped around a silly programming problem. I was trying to find the most elegant way to work with nested tags in a jsp. I wanted a tag that could either be standalone with an object passed in as a param or nested inside of other tags and retrieve the object from the parent tag.
While this is possible, if you agree on conventions and the tags are designed to work together, one can call getParentTag() and then call methods on that tag. But, what happens when instead of being immediately nested, it’s two layers deep. You need to include logic to look at the parent’s parent. It wound up that more than half of the code for the tag was just to load the required object and this code would need to be duplicated in other tags. This also created a tight coupling between tags when the main purpose is that they are bits of reusable code and not tightly coupled.
Another option was to use the request object and pass objects in and out as attributes. This requires a convention for naming and can cause unpredictable behavior if there is a naming collision.
It would seem that there’s a need for better communication between tags. The ability for a parent tag to expose objects for child tags and vice versa. The current options just don’t feel like elegantly designed solutions. The feel really hacky and prone to falling apart further down the road.
In the end I chose to just use a scriptlet instead of the parent tag, which probably should have been the obvious choice from the beginning. Just one of those so close, yet so far feelings, but it works and that’s what matters right now.