• Post Reply Bookmark Topic Watch Topic
  • New Topic

tag attributes scope

 
Rasael Bervini
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have an issue with variables/attributes scope inside a jsp tag file.

In short, I have a tag with an attribute named "id". If the page using my tag has a variable called "id" (maybe coming from the spring model) and I call my tag WITHOUT specifying the id attribute, inside my tag I still can acces to the "id" attribute that was defined in the page but I don't want this behavior; if the tag is called without the "id" attribute then it should defaults to empty/null.

Example:

print.tag


site.jsp

(...)

What I want is to have the tag attributes live only in the tag, without having any knowledge of any variable outside of the tag itself.
Is it possible?


My current workaround is to remove the "id" attribute, enable dynamic attributes and with a scriptlet search in the dynamic attributes map for the "id" and save it in a variable with a different name (e.g. "__id").
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35728
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I didn't know it did that either. I don't suppose you have enough control over the callers to have them pass:

 
Rasael Bervini
Greenhorn
Posts: 2
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it seems there is no simple solution to this.. A workaround could be, as Jeanne pointed out, to define all the attributes in the tag but that would make the tag impossible to manage as it has about 30 attributes..

The perfect solution would be to add an attribute to the 'attribute' tag directive to allow the scope of the attribute in the tag only. The attribute could simply be a 'defaultValue' and therefore would resolve any name conflict issues..

However I can't wait for a perfect solution so what I have implemented is:
- Remove all non-required attribute directives from the tag
- Create a DynamicAttributesExtractor to extract variables from the dynamic attributes map and place them into a new map that is pushed to the current context. This extractor has the ability to keep or remove the attribute from the dynamic attributes map, tracks the added objects to the context for future removal and also manages the automatic resolution of message related attributes (e.g. spring's message code) .
- Extract the attributes that I need in my tag from the dynamic attributes, any other attribute will be kept in the dynamic attributes for normal operation of that feature.
- in the tag, instead of referring to the attributes directly (e.g. ${id}) I use the map instead (e.g. ${attrs['id']}).
- at the end of the tag I release the map from the context to prevent name clashes further down the runtime

PS. Dynamic attributes are the deal! this is the second time I beat spring/jsp this month! :-) I had another issue where I needed to set dynamic attributes for a spring's form:input tag (e.g. the input id) and there is no way to do that with jsp. The solution was to get all attributes as dynamic attributes in a jsp tag, then call my custom tag (that is an extension of spring's InputTag) passing the dynamic attributes as a known attribute 'attributesMap' and there inject the attributes from the map inside the InputTag dynamic attributes.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35728
412
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I gave you a cow for posting your solution. It is surprising there isn't a simple solution, but as neither of us were able to find one with research...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!