I. Preface (translated from Orion official site)
This article will introduce how to define your own tag under Orion Application Server step by step, implement the tag function, and finally use a jsp Example for testing.
2. Create a tag to implement the tag function.
1. Name package: com. acme. mytags (to be the same as the original one, I will not change it)
Package com. acme. mytags;
2. import related class
Import javax. servlet. jsp .*;
Import javax. servlet. jsp. tagext .*;
3. Implement the javax. servlet. jsp. tagext. Tag interface:
Public class HelloWorld implements Tag
{
4. Define local variables
Private PageContext pageContext;
Private Tag parent;
5. Implementation Method for calling the label
Public int doStartTag () throws javax. servlet. jsp. JspException
{
Return SKIP_BODY;
}
Note: SKIP_BODY is returned in this method. If the label body is empty, this value is returned. Otherwise, the value EVAL_BODY_INCLUDE is returned.
6. Call the implementation method of tag termination
Public int doEndTag () throws javax. servlet. jsp. JspException
{
Try
{
PageContext. getOut (). write ("Hello World! ");
}
Catch (java. io. IOException e)
{
Throw new JspException ("IO Error:" + e. getMessage ());
}
Return EVAL_PAGE;
}
In this way, "Hello World!" will be output at the end of the custom tag of the jsp file !"
7. Not enough. We need to write down the following method:
Public void release (){}
For this simple example, the above method does not require any implementation.
8. The JSP Container needs to call the following method:
Public void setPageContext (final javax. servlet. jsp. PageContext pageContext ){
This. pageContext = pageContext;
}
The JSP Container uses the preceding method to call the tag, and the preceding method is used to set the PageContext of the tag.
9. The JSP Container also calls the following method:
Public void setParent (final javax. servlet. jsp. tagext. Tag parent)
{
This. parent = parent;
}
The JSP Container uses the preceding method to set the parent-Tag of the Tag, because the PageContext of each Tag must retain its parent Tag.
10. Finally, the implementation method is as follows:
Public javax. servlet. jsp. tagext. Tag getParent ()
{
Return parent;
}
}
11. Compile tags.
Iii. descriptive tags
Now we will write an Tracing file to describe the tag.
1. Create a taglib. tld file,
2. taglib. tld is an XML text file. The XML header is as follows:
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE taglib PUBLIC "-// Sun Microsystems, Inc. // dtd jsp Tag Library 1.1 // EN"
Http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd>
3. Describe the tag Library
<Taglib>
<Tlibversion> 1.0 </tlibversion>
<Jspversion> 1.1 </jspversion>
<Shortname> mt </shortname> <uri> http://www.orionserver.com/tutorials/tagtut/lesson1/mytags.jar </uri>
<Info> My first Tag library </info>
4. descriptive tag
<Tag>
<Name> helloWorld </name>
<Tagclass> com. acme. mytags. HelloWorld </tagclass>
<Bodycontent> empty </bodycontent>
<Info> A Hello world Tag </info>
</Tag>
5. End
</Taglib>
Iv. Packaging
Name: mytags. jar
Its directory structure is:
Com/acme/mytags/HelloWorld. class
META-INF/taglib. tld
5. Use custom tags in jsp files
Create hello. jsp as follows:
<% @ Taglib uri = "mytags. jar" prefix = "mt" %>
<HTML>
<HEAD>
<TITLE> Hello World! </TITLE>
</HEAD> <body bgcolor = "# FFFFFF">
<HR>
<Mt: helloWorld/>
<HR>
</BODY>
</HTML>
Vi. Test Run
In the orion directory, create the following structure, where the tag is created by yourself, and the preceding directory is originally available.
E: \ orion \ default-web-app \ tag
Put all jar files and jsp files in this directory.
Then, access:
Http: // localhost: [port]/tag/hello. jsp
:
--------------------------------------------------------------------------------
Hello World!
--------------------------------------------------------------------------------
7. Congratulations, you have succeeded!
Appendix: mytags. jar and hello. jsp files site:
Http://www.wodejia.net/softdownload/java/orion_tag01.zip