1) Function: When the JSP's built-in label or JSTL does not satisfy us, then we need to customize the label
2) Three steps: Requirements: Export the customer's IP address using a custom label
Simpletag interface Method:
Dotag (): Method to execute the label, called by the Web server
GetParent (): Gets the parent tag.
Setjspbody (jspfragment jspbody) incoming label body content. Called by the Web server
Setjspcontext (Jspcontext pc) incoming PageContext. Called by the Web server
SetParent (jsptag parent) is passed into the Parents tab. Called by the Web server
A) write a Java class that implements the Simpletag interface or inherits the Simpletagsupport class.
Label Processing class
public class Showclientiptag extends simpletagsupport{
@Override
public void Dotag () throws Jspexception, IOException {
Get PageContext Object
PageContext pc = (PageContext) getjspcontext ();
ServletRequest request = Pc.getrequest ();
JspWriter out = Pc.getout ();
String IP = request.getremoteaddr ();
Out.write (IP);
}
}
b) Configure this tab
Create a new TLD file under the Web-inf directory under the project. (ITCAST.TLD)
<taglib xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version= "2.0" >
<tlib-version>1.0</tlib-version>
<short-name>itcast</short-name>
<uri>http://gz.itcast.cn/jsp/tags</uri>
<!--a label configuration--
<tag>
<name>showClientIP</name>
<tag-class>gz.itcast.tag.ShowClientIPTag</tag-class>
<!--default empty: Indicates an empty label, which has body content--
<body-content>empty</body-content>
</tag>
</taglib>
c) Use the label
Import: <% @taglib uri= "http://gz.itcast.cn/jsp/tags" prefix= "Itcast"%>
Use: <itcast:showClientIP/>
You must practice: Customize label steps
3) Custom Label execution process (principle)
Setjspcontext ()
SetParent ()
Setjspbody ()
Dotag ()
4) Use a custom label (usage)
4.1 Control whether the label body content is executed
Execution: Jspframent.invoke (NULL)
Do not do: do nothing!
? 4.2 Whether the content is executed after controlling the label
Execute: Do nothing!
Do not execute: throws a Skippageexception
? 4.3 Control label body content re-execution
for (int i = 1; I <= 3; i++) {
Getjspbody (). Invoke (null);
}
4.4 Labels with attributes
A) Label processing class requires a setter method
public void SetCount (int count) {//Setter property name: Count
This.count = count;
}
b) Declare this attribute in the TLD file
<tag>
<name>demo3</name>
<tag-class>gz.itcast.tag.SimpleDemo3</tag-class>
<body-content>scriptless</body-content>
<!--property Configuration--
<attribute>
<!--property name--
<name>count</name>
<!--is required
<required>true</required>
< does!--support El expression--
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
? 4.5 Control label Body content changes
1) Get the main content
Jspfragment jspbody = Getjspbody ();
2) Used for temporary container object put body content
StringWriter SW = new StringWriter ();
3) put the subject content in a temporary container
Jspbody.invoke (SW);
4) Get the main content from the temporary container
String content = sw.tostring ();
5) Change
Content = Content.touppercase ();
6) Direct output to browser
Getjspcontext (). Getout (). write (content);
Custom labels for jstl