JSP / Custom Tag Libraries

J

JStrummer

I am writing my first page in JSP and am trying to get my arms around
custom tag libraries.

What I'd like to do is mimic an ASP-type subroutine here, where I call
a subroutine, pass in a parameter, and output HTML code. For instance,
in ASP, writePageTitle("My homepage") would yield <TITLE>My
homepage</TITLE>.

I am having problems passing the parameter to a tag file in JSP. Here
is what I have. I would appreciate any pointers, and, if I am going
about this the wrong way, I am very, very open to suggestion. Thanks
in advance.

index.jsp
==================
<HTML>
<HEAD>
<c:set var="PageKeywords" value="Home, Homepage, Main Page" />
<c:set var="PageTitle" value="Home" />

<%@ taglib prefix="HTMLTitle"
tagdir="/WEB-INF/tags" %>
<HTMLTitle:html_title/ PageTitle="<%=PageTitle%>">

and so on....

html_title.tag
===================
<%
out.println("<TITLE>${PageTitle}</TITLE>");
%>
 
S

Sudsy

JStrummer said:
I am writing my first page in JSP and am trying to get my arms around
custom tag libraries.

What I'd like to do is mimic an ASP-type subroutine here, where I call
a subroutine, pass in a parameter, and output HTML code. For instance,
in ASP, writePageTitle("My homepage") would yield <TITLE>My
homepage</TITLE>.

I am having problems passing the parameter to a tag file in JSP. Here
is what I have. I would appreciate any pointers, and, if I am going
about this the wrong way, I am very, very open to suggestion. Thanks
in advance.

Oh boy! You are so far away from how custom tags work that I would
suggest running, not walking, to your nearest computer book store
and picking up "Advanced JavaServer Pages" by David Geary (ISBN
0-13-030704-1).
First of all, your taglib statement should read like this:
<%@ taglib uri="some_uri" prefix="some_prefix" />
Then, some_uri has to be declared in your web.xml file, vis:
<taglib>
<taglib-uri>some_uri</taglib-uri>
<taglib-location>some_file.tld</taglib-location>
</taglib>
In that file you will find the tag definitions, hence the tld
(tag library definition) file extension. There you will find
the connection to the actual Java class, something like this:
<tag>
<name>some_tag_name</name>
<tagclass>tag_class_name</tagclass>
...
</tag>
Finally, you'll have to find tag_class_name in one of the
jars known to the web application, i.e. in WEB-INF/lib, or
as a stand-alone file in the WEB-INF/classes directory tree.
This class will typically extend either TagSupport or
BodyTagSupport...
There's a lot at play here. You're trying to use the facility
as if it was simply a matter of including content. Sorry, but
it's a bit more complicated than that.
 
R

Ryan Stewart

Sudsy said:
Oh boy! You are so far away from how custom tags work that I would
suggest running, not walking, to your nearest computer book store
and picking up "Advanced JavaServer Pages" by David Geary (ISBN
0-13-030704-1).
Ditto that.

*snip*
In that file you will find the tag definitions, hence the tld
(tag library definition) file extension. There you will find
Actually, tag library descriptor.

*snip*
There's a lot at play here. You're trying to use the facility
as if it was simply a matter of including content. Sorry, but
it's a bit more complicated than that.
Ditto again. In my experience, JSP is almost infinitely more structured than
ASP. You can't just sling parts together and expect it to give some result.
 
K

kaeli

Oh boy! You are so far away from how custom tags work that I would
suggest running, not walking, to your nearest computer book store
and picking up "Advanced JavaServer Pages" by David Geary (ISBN
0-13-030704-1).

I agree.
I found this site to be very useful when I was learning taglibs. It has
usable code to play with, too.

http://www.orionserver.com/tutorials/taglibs/

--
--
~kaeli~
Jesus saves, Allah protects, and Cthulhu thinks you'd make
a nice sandwich.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top