Vous êtes sur la page 1sur 44

Java II--Copyright © 2001-2005 Tom Hunter

J2EE
JSP Custom Tag Libraries

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
Custom Tag Libraries

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Custom Tag Libraries
• Since JSP 1.1 there has been a very powerful tool called
custom tag libraries.
• The idea is to make complex server-side behavior available
for use on a JSP page using a very basic syntax.
• We have glimpsed this power in the jsp:useBean
combination we explored in the previous lecture.
• One major advantage of custom taglibs is their ability to
manipulate JSP content.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Custom Tag Libraries
• To use custom JSP tags, you need to define three separate
components:
1.) The tag handler class—this defines the tag’s behavior.
2.) The tag library descriptor file—that maps the XML
element names to the tag implementation.
3.) The JSP file that uses the tag library.

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
Tag Handler Class

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Tag Handler Class
• First of all, this is just another Java class.
• When you want to define a new tag, you must first write a
Java class that does the work you expect your tag to do.
• This class must implement the

javax.servlet.jsp.tagext.Tag interface.

• Usually, this is accomplished by extending the


TagSupport or BodyTagSupport classes.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Tag Handler Class
• So, without further adieu, let’s look at an example:
Notice, I’ve put the tag handler
package mypackage; class in a package, so that will be a
directory inside the classes
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*; directory.
import java.io.*;

public class FirstTag extends TagSupport If you think about XML, it


{ always has a Start Tag and an
public int doStartTag()
{
End Tag. Usually—but not
try always—there is a body between
{ the tags. In this case, we’re just
JspWriter out = pageContext.getOut(); giving a start tag.
out.print( “My First Tag” );
}
catch( IOException io )
{
System.out.println( “FirstTag threw an IOException io” );
}
return SKIP_BODY;
} When we return this constant int, “Skip any text you
} find between the body of the tag.”
Java II--Copyright © 2001-2005 Tom Hunter
JSP: Tag Handler Class
• After we have written our tag handler class, we need to
compile it and place it in a directory below the
WEB-INF/classes directory.
Since we placed our
FirstTag class in a
package, we have to
account for that package in
the directory below
classes.

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
Tag Library Descriptor
File
Java II--Copyright © 2001-2005 Tom Hunter
JSP: Tag Library Descriptor File
• So, we’ve finished with step 1, creating the class.
• Next, we have to announce the tag to the server.

JavaclassTaglib.tld The “shortname” is the library


name, which will appear to the
left of the colon.

The tag’s “name” is the part


that will appear to the right of
the colon.

This is the Java class that


implements the tag. The class is
usually placed in a package.

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
A JSP That Uses The Tag

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A JSP That Uses The Tag
• Now, we have:
 the Tag Handler class
 a Tag Library Descriptor File
and we still need
 the JSP.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A JSP That Uses The Tag
• Here is the JSP that we will use to execute our first tag.
• Just like any other JSP, we place it in the javaclass
directory. The .tld file we just created
must be referenced here at the
TestFirstTag.jsp
top in a taglib Directive.

The prefix parameter tells our


JSP which “shortname” prefix
to look for inside the .tld file.

Since the URI for our .tld file


does not specify a path, then we
are saying it will be found in the
same directory as the JSP.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A JSP That Uses The Tag
• Let’s review the process.

1.) Write the tag handler class. Compile. Put in


WEB-INF/classes directory in the correct package.

2.) Write the Tag Library Definition file (.tld), place in


javaclass directory.

3.) Write the JSP, place in javaclass directory. The JSP


must have the taglib directive before the tag is used.

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
A More Ambitious Tag

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
• As the previous tag did not seem worth the trouble, we
will look at one that begins to show you how valuable these
taglibs are.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
• For the price of a tiny tag, this will insert a table into our
JSP.
• Let’s remember our three steps:

1.) Tag Handler class


2.) Tag Library Descriptor File
3.) JSP that uses the taglib.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag

Please note that I had to


“escape” the double quotes so
that the class was able to
compile.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
JavaclassTaglib.tld

I next add the tag to the


table definition library.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
• Finally, here is the JSP that uses this new tag.
TestTableTag.jsp

I don’t know about you, but


this part looks pretty easy.
And, in a normal
environment, all of these
tags would have already
been written.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
• This is the resulting page.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
• Recall that our doStartTag() method ended with the
return value SKIP_BODY, which tells the tag to skip
anything it found between the start and end tags. Let’s see if
that really does what we expect.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: A More Ambitious Tag
• We change our JSP so that it includes an end tag.

from TableTag.java

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
More on the TLD

Java II--Copyright © 2001-2005 Tom Hunter


JSP: More on the TLD
• For simplicity, I omitted one of the tags that you should
include if your body content is supposed to be empty.

• If the tag is expecting normal JSP content in the body,


you use this:

• If the tag takes care of its own body:

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
Assigning Attributes to
Tags
Java II--Copyright © 2001-2005 Tom Hunter
JSP: Assigning Attributes to Tags
• Although our examples so far have not used them, it is
common to pass attributes to tags so the values of the
attributes can be incorporated into the HTML that is
generated in the tag.

<prefix:name attribute1=“value1” attribute2=“value2” />

• This is where our knowledge of the ways of JavaBeans


comes in handy, because that’s exactly the way it works.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Assigning Attributes to Tags
• The tag handler that corresponds to the tag below would
have to have methods named:

public void setAttribute1( String value1 )

and

public void setAttribute2( String value2 )

<prefix:name attribute1=“value1” attribute2=“value2” />

• When the tag gets processed, it will automatically call these


methods in the tag handler class.
Java II--Copyright © 2001-2005 Tom Hunter
JSP: Assigning Attributes to Tags
• Here we see how the method getMyAttribute() is inserted
into the tag.

• Also, notice how


nowhere does this
class call
“setMyAttribute()”

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Assigning Attributes to Tags
• We’re not done yet—we need to add our
ParameterTag class to the tld file.
When you have chosen to
include an attribute in your
tag, you need to add the
attribute tag, which itself
has three possible sub
elements.

The name here is case


sensitive. Remembering the
rules of JavaBeans, it must
match the getters and
setters in your class.

This optional attribute rtexprvalue shows


whether it is okay for the value to be the result
of a JSP Expression.
<%= expression %>
Java II--Copyright © 2001-2005 Tom Hunter
JSP: Assigning Attributes to Tags
• Last but not least we build a JSP to take advantage of our
tag.
See how this is going to
work? Whatever we insert in
myAttribute gets
inserted into the tag when
it’s made.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Assigning Attributes to Tags
• Granted, these have been pretty simple examples.

• But, hopefully, you can see how we could pass in multiple


attributes and get some pretty complex structures.

• The tags give us canned code.

• Everybody shares the same code base and nothing is hard-


coded in our JSPs.

Java II--Copyright © 2001-2005 Tom Hunter


JSP:
Including the Tag Body

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• Up until now, all of our Custom Tags have omitted the
body. Let’s see how the body can be included.
• Our doStartTag() methods have always returned the
constant SKIP_BODY.

return SKIP_BODY;
}

• If, instead, we would have had our doStartTag()


method return the constant EVAL_BODY_INCLUDE,
then we can have the body contain JSP scripting elements,
directives and actions.
return EVAL_BODY_INCLUDE;
}
Java II--Copyright © 2001-2005 Tom Hunter
JSP: Including the Tag Body
• To make sure you understand what I’m saying, here’s an
example:

<prefix:mytag>
body stuff can go in here including any legal JSP stuff.
</prefix:mytag>

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• Often, when you’re bothering to include the body, you also
want to do something special with the end tag.

• Naturally, there is a method you can override called:


doEndTag()

• For its return value, you can have the doEndTag()


method either:
return EVAL_PAGE; This means, after you’re done with the end tag,
or continue to evaluate the rest of the JSP page.

return SKIP_PAGE;
This means, after you’re done with the end tag,
abandon the rest of the JSP page.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• Quickly, let’s look at the Tag Handler class:

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• Here’s the Start Tag

This means the body should be evaluated.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• And, finally, here’s the End Tag.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• This is the .tld file. As you
can see, all of the attributes need
to be listed.

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
• This example, when we see how it renders, will start to
show how powerful this technique is.
HeadingExample.jsp

Java II--Copyright © 2001-2005 Tom Hunter


JSP: Including the Tag Body
HeadingExample.jsp

Java II--Copyright © 2001-2005 Tom Hunter


Package
com.hp.bco.pl.wpa.taglib.hpweb

WPA HPWeb Layout Plug-in Version 1.1.7

Java II--Copyright © 2001-2005 Tom Hunter

Vous aimerez peut-être aussi