After reading "JSP Design", it is deeply aware of the master's skill. This note is a summary.
All code is passed in Win2000 Tomcat 4.1.17.
I just started using Java, there is case of improper, please ask more advice.
Chapter 1: How to use taglib
First, the first method: only declare the TLD location
At the head of the JSP file, statement:
<% @ Taglib URI = "/ Web-INF / TLDS / ORATAGLIB_1_0.TLD" prefix = "ORA"%>
This is OK.
Second, the second method: Define the name of the library
At the head of the JSP file, statement:
<% @ Taglib Uri = "/ ORATAGLIB" prefix = "ORA"%>
Add the following statement in Web.xml:
taglib>
Plus in TLD:
Third, difference
If there is not much, it will be referenced by TLD. Otherwise, use the library form to facilitate upgrade, and change.
Chapter 2: Writing the simplest Taglib
First, write the HelloWorldTAG class
Package mytag;
Import java.io.ioException;
Import javax.servlet.jsp. *;
Import javax.servlet.jsp.tagext. *;
Public class helloworldtag extends tagsupport {
Public int dostarttag () THROWS JSptagexception {
RETURN EVAL_BODY_INCLUDE;
}
PUBLIC INT DOENDTAG () THROWS JSptagexception {
Try {
PageContext.getOut (). Write ("Hello World");
}
Catch (IOException EX) {
Throw new JSptagexception ("Error!");
}
Return Eval_page;
}
}
Second, write the corresponding TLD file
That is hello.tld
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">
taglib>
Third, write TestTAG1.JSP for call
<% @ page language = "java"%>
<% @ Taglib URI = "/ Web-INF / TLDS / Hello.Tld" prefix = "MyTag"%>
body>
html>
Fourth, pay attention
1. The actually generated class is in the package mytag, put the package into the web-inf / class directory
2. The shortname in the TLD file is the prefix in the JSP.
3. In the
4. Put the TLD file into the TLDs folder under Web-INF.
V. Overview
1. Javax.Servlet.jsp. * And javax.servlet.jsp.tagext. * Two packages
2. Let your class inherit the Tagsupport class and implement the dostartTAG or DOENDTAG method.
3. Write a TLD tag, just specify the
4. In the JSP page is an imported TLD. Then use the prefix: mode of the class name.
Chapter 3: Writing Taglib with Body
First, write the HellobodyTag class
Package mytag;
Import java.io. *;
Import javax.servlet.jsp. *;
Import javax.servlet.jsp.tagext. *;
Public class hellobodytag extends bodytagsupport {
PUBLIC INT DOAFTERBODY () THROWS JSPEXCEPTION {
Bodycontent bc = getBodyContent ();
Jspwriter out = getpreviousout ();
Try {
Out.write (bc.getstring ());
}
Catch (IOException E) {} // ignore
Return Skip_body;
}
}
Second, modify the TLD file
Join:
tag>
Third, write TestTAG2.JSP call
<% @ page language = "java"%>
<% @ Taglib URI = "/ Web-INF / TLDS / Hello.Tld" prefix = "MyTag"%>
body>
html>
Fourth, outline 1. Best inheritance of BodyTagsupport
2. Both bothContent and JspWriter are objects in the container
3. GetPreviousout indicates the outer output. For a TAG, the outer output is a JSP page. For a nested TAG, the outer output is the parent TAG
Chapter 4: Let the behavior cooperate with each other
First, write the behavior of mutual cooperation
Write such a TAG, when the JSP page is called
Will get the value of Hello Diego.
Second, write two TAG classes
1. Write paramtag class
Package mytag;
Import java.net. *;
Import javax.servlet.jsp. *;
Import javax.servlet.jsp.tagext. *;
Public class paramtag extends tagsupport {
PRIVATE STRING NAME;
Public void setname (String name) {
THIS.NAME = Name;
}
PUBLIC INT DOENDTAG () THROWS JSPEXCEPTION {
Tag Parent = FindanceStorwithClass (this, paramparent.class);
Paramparent paramparent = (paramparent) Parent;
Paramparent.SetParam (name);
Return Eval_page;
}
Public void release () {
Name = NULL;
Super.release ();
}
}
2. Write the HelloparamTag class
Package mytag;
Import java.io. *;
Import javax.servlet.http. *;
Import javax.servlet.jsp. *;
Import javax.servlet.jsp.tagext. *;
Public class helloparamtag extends tagsupport implements paramparent {
PRIVATE STRING NAME;
Public void setParam (String name) {
THIS.NAME = Name;
}
Public int desarttag () {
RETURN EVAL_BODY_INCLUDE;
}
PUBLIC INT DOENDTAG () THROWS JSPEXCEPTION {
Jspwriter out = pageContext.getut ();
Try {
Out.print ("Hello" this.name;
}
Catch (IOException E) {
}
Return Eval_page;
}
Public void release () {
Name = NULL;
Super.release ();
}
}
3. Write auxiliary interface paramparent
Package mytag;
Public interface paramparent {
Void setParam (String name);
}
Third, modify the TLD file
tag>
attribute>
tag>
Fourth, write TestTAG3.JSP for testing
<% @ page language = "java"%>
<% @ Taglib URI = "/ Web-INF / TLDS / Hello.Tld" prefix = "MyTag"%>
body>
html>
Five, design ideas
1. In nested behavior, general parent TAG implements business logic, and sub-tag provides parameters for the parent TAG.
2. The tag finds the parent TAG through FindanceStorwithClass, in order to achieve common, design interface paramparent
3. The setParam method of the interface is implemented in the parent TAG, and the sub-tag is set to the parameter of the parent TAG by calling the method.
4. The parent TAG implements business logic. Print Hello