JAVA/JSP Learning Series VII (Orion's CUSTOM tag) _jsp programming
Last Update:2017-01-18
Source: Internet
Author: User
Preface (This article is translated from the Orion official site)
This article takes a step-by-step introduction to defining your own label under the Orion application Server, then implements the tag's functionality, and finally tests it with a JSP example.
Second, the establishment of a label to achieve the function of the label.
1. Named Package: Com.acme.mytags (for the same as the original, I do not make changes)
Package com.acme.mytags;
2.import Related class
Import javax.servlet.jsp.*;
Import javax.servlet.jsp.tagext.*;
3. Implement JAVAX.SERVLET.JSP.TAGEXT.TAG Interface:
public class HelloWorld implements Tag
{
4. Define Local variables
Private PageContext PageContext;
Private Tag parent;
5. Calling the implementation of the tag Start method
public int doStartTag () throws Javax.servlet.jsp.JspException
{
return skip_body;
}
Note: This method returns Skip_body, that is, when the body of the label is empty, it returns this value, otherwise it will return: Eval_body_include
6. The implementation method of calling tag end
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;
}
This will output "Hello world!" at the end of the custom tag of the JSP file
7. Not enough, we have to write the following method:
public void Release () {}
For this simple example, the above method does not require any implementation.
8.JSP container to invoke the following method:
public void Setpagecontext (final javax.servlet.jsp.PageContext PageContext) {
This.pagecontext=pagecontext;
}
The JSP container invokes the label through the method above, and the method above is used to set the label's PageContext.
The 9.JSP container also calls the following method:
public void SetParent (final javax.servlet.jsp.tagext.Tag parent)
{
This.parent=parent;
}
The JSP container sets the label's Parent-tag by the above method, because each label's PageContext retains its parent tag.
10. Finally, the implementation method:
Public Javax.servlet.jsp.tagext.Tag GetParent ()
{
return to parent;
}
}
11. Compile tags.
Third, describe the label
Now you're going to write a description file describing the label.
1. Establishment of a TAGLIB.TLD document,
2.TAGLIB.TLD is an XML-formatted text file with the following XML headers:
<?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 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-A-Tag library</info>
4. Describe the label
<tag>
<name>helloWorld</name>
<tagclass>com.acme.mytags.HelloWorld</tagclass>
<bodycontent>empty</bodycontent>
<info>a Hello World tag</info>
</tag>
5. End
</taglib>
Four, packing
Name is: Mytags.jar
Its directory structure is:
Com/acme/mytags/helloworld.class
Meta-inf/taglib.tld
Use custom labels in your JSP files
The establishment of HELLO.JSP is 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, set up the following structure, where tag is built by itself, the previous directory is already there.
E:\orion\default-web-app\tag
Put the jar file and the JSP file all in this directory.
Then, visit:
http://localhost:[port]/tag/hello.jsp
will appear:
--------------------------------------------------------------------------------
Hello world!
--------------------------------------------------------------------------------
Congratulations, you have succeeded!
Attached: Mytags.jar and hello.jsp file download address:
Http://www.wodejia.net/softdownload/java/orion_tag01.zip