View Full Version : Adding attributes at runtime in JSP 2.0
taluk
03-26-2007, 02:22 PM
Hi,
i'm trying to wrap an existing tag with a *.tag file that i created.
i'm getting all the attributes in but i don't want to pass them all to the tag.
let's say the tag name is <foo:bar />
then i want to put attributes dynamiclly .
i cannot put a <c:if> inside the tag since it;s should be evaluated in server side and then run in server side.
(something like :
<foo:bar>
<c:if test="${! empty someAttr}">
<jsp:attribute name="someAttr">${someAttr}</jsp:attribute>
</c:if>
</foo:bar>
)
can i do it at all ?
thanks,Taluk
shyam
03-26-2007, 06:35 PM
why don't you simply check for null in the tag handling code?
Could PageContext and the setAttribute() and getAttribute() methods be what you are looking for?
If I understand what you want, something like the following could work:
In the jsp-file:
[...]
SomeObject theObject = new SomeObject("Stuff");
pageContext.setAttribute("ObjectByItsName/Key", theObject);
And in your tag-file(s), you get access to that object by doing something like this:
PageContext pageContext;
pageContext = (PageContext) getJspContext();
SomeObject myObject = (SomeObject) pageContext.getAttribute("ObjectByItsName/Key");
if (myObject != null) {
// do stuff with the object...
}
Hope that helps. :)
taluk
03-27-2007, 07:58 AM
I am trying to wrap a third party open source tag.
so i've written a *.tag file of my own that has almost all the attributes of the original tag and i added some funcionality.
when i'm using the third party tag , i can't control which attributes i'll send and which not.
and you know that not sending an attribute isn't like sending it with blank value
(someAttr="") .
i'll give some example : say the third party tag is called foo:bar and it has two attributes oneAttr and twoAttr. let's say that if i'm sending oneAttr ,the third party tag will never look at the value of twoAttr. so what i wanted to do is to see what the user sent to my tag and then send only the attributes that the user (developper in that case...) sent me.
otherwise i'll have to do all the permutations of the attributes. hence if i have N attributes i'll have to do 2^N permutations. it's fine with 2 and not so fine with 20.
i hope i cleared it up. if it's not clear yet i'll send some code piece...
thanks for the good will anyways ... :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.