• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can we set a Collection object as attribute to be used by a custom tag?

 
Vicky Mohan
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a custom tag that can be used across our system. I wanted to get a collection of Objects available in HttpServletRequest, for the tag to process. I wanted to pass that collection as an attribute.

I can directly get the collection from request by using pageContext . But i do not want to do it that way, as reusability might be affected.

How do i pass the collection to my custom tag ???

[ November 10, 2004: Message edited by: Mohen Vijay ]
[ November 11, 2004: Message edited by: Bear Bibeault ]
 
Sean Stephens
Ranch Hand
Posts: 40
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The way I understand what your asking for is you want something that works like this:

<myTagLib:customTag collectionAttribute="<%= myCollection %>" />

And you say myCollection is accessible from the request object in the page. I've done this two ways, one is passing the request to customTag as the attribute instead of the collection, so:

<myTagLib:customTag myRequest="<%= request %>" />

and getting the collection in the class and doing what I need to. However, this is a heavy handed and somewhat sloppy way to do it IMO.

The other way I've done it is to use a jsp:useBean tag to get the object out of the session/request and then pass it to the custom tag as an attribute like this:

<jsp:useBean id="myCollection" class="myCollectionClassname" scope="" />
<!-- define scope above as request or session depending on where the actial bean was placed as an attribute -->

<myTagLib:customTag collectionAttribute="myCollection" />

Hope I understood your question right and that this little blurb helps.

- Sean
 
Vicky Mohan
Ranch Hand
Posts: 130
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply ,Sean.

<myTagLib:customTag myRequest="<%= request %>" />

I dont want to do it this way as i can get the request object directly from PageContext in the CustomTag Class.

Regarding the other option

<jsp:useBean id="myCollection" class="myCollectionClassname" scope="" />
<myTagLib:customTag collectionAttribute="myCollection" />

I am thinking this would still return a String and we might have to use this string as the name of our collection.

From my understanding, there is no way we can pass on an Object ( of course, other than String) to a Custom Tag.

please correct me if i am wrong.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic