jsp-defines a label with a tagged body __js

Source: Internet
Author: User
Tags tld
jsp-defines a label with a tagged body--loop
So far, we have defined a custom label with no label. To define a class with a label to inherit the Bodytagsupport class, Bodytagsupport extends the TagSupport class, so the doStartTag and Doendtag methods are the same as the previous one, In addition to these methods, the Bodytagsupport class also defines two important methods.
1:doafterbody-This method is executed each time the label body is processed. You can overload this method to handle your own label body, which usually returns skip_body at the end, indicating that no label body needs to continue processing, and if this method returns Eval_body_tag, the label body is processed again, causing a doafterbody call, This process will continue until Doafterbody returns Skip_body
2:doinitbody--method of labeling body initialization.
In addition, Bodytagsupport defines a member variable bodycontent, the type is bodycontent. Bodycontent is a subclass of JspWriter that allows access to the label body, which stores the results of the processing of the label body, and the main methods of the class are as follows
1:getenclosingwriter--is used to return JspWriter objects used by the doStartTag and Doendtag methods.
2:getreader--will return a reader can be used to read the label body
3:getstring---Returns the string contained in the entire label body
4:clearbody--empty the stored label body processing results

Here's an example to get a grip on this type of label.
The Looptag.java code is as follows
Package tag.test.date;
Import javax.servlet.jsp.*;
Import javax.servlet.jsp.tagext.*;
Import java.io.*;
public class Looptag extends bodytagsupport{
String UserName;
int count;
Public Looptag () {
Super ();
}
public void Setusername (String userName) {
This.username=username;
}
public void SetCount (int count) {
This.count=count;
}
public void Doinitbody () throws jsptagexception{
System.out.println ("Doinitbody ()");
}
public int doStartTag () throws jsptagexception{
System.out.println ("doStartTag");
if (count>0)
{
return eval_body_tag;
}
Else
{
return skip_body;
}
}
public int doafterbody () throws jsptagexception{
System.out.println ("Doafterbody ()");
if (count-->=1)
{
Try
{
JspWriter Out=bodycontent.getenclosingwriter ();
Out.println (bodycontent.getstring () +username);
Out.println ("<br>");
Bodycontent.clearbody ();
}
catch (IOException E)
{
E.printstacktrace ();
}
return (Eval_body_tag);
}
Else
{
return (Skip_body);
}
}
public int Doendtag () throws jsptagexception{
System.out.println ("Doendtag ()");
return eval_page;
}
}
After compiling this file, copy the resulting class to the C:/tomcat/webapps/root/web-inf/classes/tag/test/date directory.

Then write the. tld file code, as follows
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<taglib>
<tlib-version>1.2</tlib-version>
<jsp-version>1.2</jsp-version>
<shor-name>mytag</shor-name>
<description>the Info Test example</description>
<tag>
<name>loop</name>
<tag-class>tag.test.date.loopTag</tag-class>
<body-content>jsp</body-content>
<attribute>
<name>userName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
Note here that the content in <body-content>jsp</body-content> must be a JSP

Note that this file is stored in a path of C:/tomcat/webapps/root/web-inf

Then modify the Web.xml file, which means adding the following to the Web.xml
<taglib>
<taglib-uri>/loop</taglib-uri>
<taglib-location>/WEB-INF/loopTag.tld</taglib-location>
</taglib>

The last is to write the looptag.jsp file, the code is as follows
<%@ taglib uri= "/loop" prefix= "MyTag"%>
<title>custom tag</title>
<body>
<mytag:loop username= "Hua" count= "3" >
Hello I ' m
</mytag:loop>
</body>

Note: The <%@ taglib uri= "/loop" prefix= "mydate"%>uri values in the JSP code must be web.xml in the <taglib-uri>/loop</taglib-uri > values are consistent.
After doing the above work, start Tomcat, and after you enter http://localhost:8080/loopTag.jsp in the Address bar, you will see the following results
Hello I ' m hua
Hello I ' m hua
Hello I ' m hua
Well, through the above explanation you must have a certain understanding of the JSP label with a custom label, and now do the experiment.
If you have any questions, please contact qq:154174583.

You can use this link to quote this article: http://aspjavavbc.bokee.com/viewdiary.18893261.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.