File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes How can we set a Collection object as attribute to be used by a custom tag? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "How can we set a Collection object as attribute to be used by a custom tag?" Watch "How can we set a Collection object as attribute to be used by a custom tag?" New topic
Author

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

Vicky Mohan
Ranch Hand

Joined: Oct 14, 2004
Posts: 130
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

Joined: Oct 25, 2004
Posts: 40
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

Joined: Oct 14, 2004
Posts: 130
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How can we set a Collection object as attribute to be used by a custom tag?