Custom Label:
1. Modify the Web.xml file to add support for the custom label:
<?xml version= "1.0" encoding= "UTF-8"?>
<web-app version= "2.5"
Xmlns= "Http://java.sun.com/xml/ns/javaee"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
Xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee
Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd ">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<taglib>
<taglib-uri>/tld/helloworld</taglib-uri>
<taglib-location>/WEB-INF/tlds/helloworld.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
---------------------------------------------------------------------------------
----
2. Create a TLD folder under Meta-inf and create helloword.tld files
<?xml version= "1.0" encoding= "UTF-8"?>
<! DOCTYPE taglib Public
"-//sun Microsystems, Inc.//dtd JSP Tag Library 1.2//en"
"Http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd" >
<taglib>
<tlib-version>1.0</tlib-version><!--The version of the tag library-->
<jsp-version>1.2</jsp-version><!--JSP specification version required for this tag library-->
<short-name>mytag</short-name><!--JSP page authoring tool can be used to create an optional name for a help-bearer-->
<tag>
<name>helloworld</name><!--Unique label name-->
<tag-class>com.credream.mytag.HelloWorldTag</tag-class><!--Label Helloword class of complete
----------
Note here: The version matching of the header information of the two XML files Web.xml and Xxx.tld is important, or it will cause the page
The error cannot be found on the label.
3. Create label handler class Helloworldtag (rewrite doStartTag and Doendtag methods).
Package Com.credream.mytag;
Import java.io.IOException;
Import javax.servlet.jsp.JspException;
Import javax.servlet.jsp.JspTagException;
Import Javax.servlet.jsp.tagext.TagSupport;
/**
* The difference between TagSupport and Bodytagsupport:
* Mainly see whether the label processing class to read the contents of the tag body and change the content returned by the label, if you do not need
Use TagSupport, or you'll use Bodytagsupport.
* The tags implemented with TagSupport can be implemented with Bodytagsupport, because
Bodytagsupport inherited TagSupport.
*/
public class Helloworldtag extends TagSupport {
@Override
public int Doendtag () throws Jspexception {
try {
Pagecontext.getout (). Write ("Hello world!!!");
catch (IOException e) {
E.printstacktrace ();
throw new Jsptagexception ("error when calling the Doendtag method"
... ");
}
return eval_page;
}
@Override
public int doStartTag () throws Jspexception {
return eval_body_include;
}
----------------
At this time to pay attention to the meaning of some parameters:
Add:
The doStartTag () method is the method that will call when the label starts, and its valid return value is Eval_body_include and
Skip_body, the former means that the text between the labels is displayed, which means that the text between the labels is not displayed.
The Doendtag () method is the method of calling at the end of the tag, and its valid return value is Eval_page and Skip_page
, which means that the following JSP pages continue to be executed after the label is processed, which means that the next JSP page is not processed.
Doafterbody (), which is called after the text is displayed, with the return value of Eval_body_again and
Skip_body, the former displays the text between the labels again, while the latter continues to perform the next step in the label processing.
Eval_body_include: Read the body into the existing output stream, the doStartTag () function is available.
Eval_page: Continue processing the page, Doendtag () function is available.
Skip_body: Ignores processing of the body, doStartTag () and Doafterbody () functions are available.
Skip_page: Ignores processing of the remaining pages, Doendtag () function is available.
Eval_body_buffered: Application buffer, which is processed by the Bodycontent object obtained by the Setbodycontent () function
The body of the tag, if the class implements the Bodytag, then doStartTag () is available, otherwise illegal.
Eval_body_again: Request to continue processing the body, returned from Doafterbody (), this return value in your make loop tag
Time is very useful.
The scheduled processing order is: doStartTag () return Skip_body,doafterbodytag () return
Skip_body,doendtag () returns Eval_page. If you inherit TagSupport, if you do not overwrite any of the parties
method, the execution order of the label processing is: doStartTag ()-> does not display the text->doendtag ()-> executes the next network
Page. If you overwrite doStartTag (), you must specify a return value, and if Eval_body_include is specified, the
The row order is: doStartTag ()-> display text->doafterbodytag ()->doendtag ()-> execute the following Web page.
---------------------------------------------------------------------------------
---------------------------------------------------------------------
4. Write Test Web hello.jsp
<%@ page language= "java" import= "java.util.*" pageencoding= "Utf-8"%>
<%@ taglib uri= "/tld/helloworld" prefix= "MyTag"%><!--declared on the page-->
<%
String path = Request.getcontextpath ();
String basepath = request.getscheme () + "://" +request.getservername
() + ":" +request.getserverport () +path+ "/";
%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
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