• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing custom tags.

 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My new project requires us to write our own JSP tags, I am stuggling to find resources to help me out on this ... anyone have any suggestions?
I think I'm doing pretty well with what I have but I have one problem. One of the attributes to my tag is "id" and I want the value of this attribute to be avialable as a scripting variable in my JSP after the tag has done it's thing. Also my tag is supposed to go off and do some stuff then come back with a Collection Object ... i want the scripting variable to refer to that object.
I know it's possible because I've seen it done in other tags ... but i don't have access to the source from the relevent class files.
Can anyone help me out with this?
 
Travis Gibson
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fellow Rancher,
Core Servlets and JSP has an excellent chapter on creating custom tags. I'd pick one up if I were you. I didn't bring my copy into the office.
In fact you can go to their website at
http:www.coreservlets.com
And download some custom tags samples.
Hope I was of some assistance,
Travis M Gibson, SCJP
 
Scott Stirling
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I do sometimes is put the id variable in the pageContext, set to the value of the tag handler itself. This allows users to access the tag handler's properties (i.e., JavaBean properties) using the standard <jsp:getProperty> tag.
Example (from the tag handler, after the properties have values):
pageContext.setAttribute(id, this);

And in the JSP:
<tag:foo id="taghandle">
<jsp:getProperty name="taghandle" property="collection">
</tag:foo>

I and many others have contributed tags to the Jakarta Taglib project. They're all open source and free. See also the tags in the Jakarta Struts project.
http://jakarta.apache.org/taglibs
http://jakarta.apache.org/struts

[This message has been edited by Scott Stirling (edited March 06, 2001).]
 
Cynthia Yao
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You may need to use extrainfo class to define several page level attribute or just declare those valuables in the jsp, then give them value during the process of tag information
 
Scott Stirling
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good point. I should have added that. When you want to create a scripting variable on the page from a tag handler, you need to declare it in a TagExtraInfo class. here's an example of how to do it with an "id" attribute that'll only be available nested between opening and closing tags:

This defines a scripting variable named "id" for the tag handler "foo.bar.taglib.fooTag". It's "true" that it will be created by the tag handler, and its scope is NESTED -- i.e., it is only available/"visible" when used nested between opening and closing tags on the page.
Other possible scopes are AT_END, which means only after the closing of the tag's appearance on the page to (potentially) the end of the document, and AT_BEGIN, which means from the beginning of the tag's first appearance on the page.
Like I said, see the Jakarta taglibs. There are several examples of this sort of thing there.
------------------
Scott Stirling
Author of Java Server Pages Application Development
[This message has been edited by Scott Stirling (edited March 06, 2001).]
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys,
Special thanks to Scott ... I'll take a look at the Jakarta examples. The most frustrating thing about this is that I'm writing a tag for use with Weblogic personalization Server, it comes with it's own perfectly good and very useful tag library .. however the powers that be here have decided that it isn't good enough so we are having to write new tags that essentially do the same thing .. with maybe a little tweaking here and there!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic