JSP Custom Label Implementation

Source: Internet
Author: User
Tags return tag tld

Example of displaying dates by custom labels

(i) no text tag implementation

(1): Define label Processing class

Import java.io.IOException;
Import Java.util.Date;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.tagext.Tag;
Import Javax.servlet.jsp.tagext.TagSupport;
No body tag class inherits the TagSupport class implementation of the interface is tag. If the tag class with the body inherits the interface implemented by the Bodytagsupport class is Bodytag
public class Datetagnobody extends TagSupport {
@Override
public int doStartTag () throws Jspexception {
HttpServletRequest request;
is a property defined in the TagSupport class that is an object of Javax.servlet.jsp.PageContext
Request = (HttpServletRequest) pagecontext.getrequest ();
Java.text.SimpleDateFormat formater = new Java.text.SimpleDateFormat ("Yyyy-mm-dd");
String date = Formater.format (new date ());
JspWriter out = Pagecontext.getout ();
try {
Out.print (date);
catch (IOException e) {
E.printstacktrace ();
}
The doStartTag () method returns Skip_body. The reason, of course, is that our simple date tag has no body.
return tag.skip_body;
}
}

(2) Defining TLD Files

<?xml version= "1.0" encoding= "UTF-8"?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<tag>
<name>displayDate</name>
<tagclass>cn.com.chenlly.tag.DateTagNoBody</tagclass>
<bodycontent>empty</bodycontent>
</tag>
</taglib>

(3) JSP page dynamic Reference

<%@ page language= "java" pageencoding= "UTF-8"%>
<%@ taglib uri= "/web-inf/datetag.tld" prefix= "C"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<body>
<c:displayDate/>
</body>

Note: The difference between a dynamic reference and a static reference.

In order to make a static reference, you must first add the following item to the Web.xml file:
<?xml version= "1.0" encoding= "Iso-8859-1"?>
<Web-app>
<taglib>
<taglib-uri>myTags</taglib-uri>
<taglib-location>/WEB-INF/lib/DateTagLib.tld</taglib-location>
</taglib>
</Web-app>
Then, add the JSP declaration to all pages that need to use the Custom tag library:
<%@ taglib uri= "Mytags" prefix= "C"%>
The specified URI property matches the Taglib-uri value specified in the Web.xml file.

When you make a static reference to a tag library, the JSP declaration must query the Web.xml file to execute the library query. This means that if you move or rename the library, or if you want to add more libraries to the Web.xml file, you must stop the server, update the Web.xml file, and then restart the server. The dynamic method makes the JSP page point directly to the TLD location and is processed when the JSP page is interpreted.

(ii) without the text but with the attributes of the label implementation

(1): Define label Processing class

Import java.io.IOException;
Import Java.util.Date;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import Javax.servlet.jsp.tagext.Tag;
Import Javax.servlet.jsp.tagext.TagSupport;
No body tag class inherits the TagSupport class implementation of the interface is tag. If the tag class with the body inherits the interface implemented by the Bodytagsupport class is Bodytag
public class Datetagnobody extends TagSupport {

Private String pattern;
@Override
public int doStartTag () throws Jspexception {
HttpServletRequest request;
is a property defined in the TagSupport class that is an object of Javax.servlet.jsp.PageContext
Request = (HttpServletRequest) pagecontext.getrequest ();
Java.text.SimpleDateFormat formater = new Java.text.SimpleDateFormat (pattern);
String date = Formater.format (new date ());
JspWriter out = Pagecontext.getout ();
try {
Out.print (date);
catch (IOException e) {
E.printstacktrace ();
}
The doStartTag () method returns Skip_body. The reason, of course, is that our simple date tag has no body.
return tag.skip_body;
}

Setxx () method must be implemented
public void Setpattern (String pattern) {
This.pattern = pattern;
}
}

(2) Defining TLD Files

<?xml version= "1.0" encoding= "UTF-8"
<taglib>
   <tlibversion>1.0</ Tlibversion>
   <jspversion>1.1</jspversion>
  <tag>
     <name>displaydate</name>
    <tagclass> Cn.com.chenlly.tag.datetagnobody</tagclass>
    <bodycontent>empty</ Bodycontent>
    <!--definition properties;
    <attribute>
        <name>pattern</name> <!--property name;
        <type>String</type>  <!--attribute types;
       < Requried>false</requried> <!--must be;
       <rtexprvale> False</rtexprvale> <!--Indicates whether JSP expressions can be used  
  </ATTRIBUTE>
  </tag>
</taglib>

(3) JSP page dynamic Reference

<%@ page language= "java" pageencoding= "UTF-8"%>
<%@ taglib uri= "/web-inf/datetag.tld" prefix= "C"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<body>
<c:displaydate pattern= ' Yyyy-mm-dd '/>
</br>
<c:displaydate pattern= ' mm/dd HH:mm:ss '/>
</body>

(iii) with the text of the label with the implementation of attributes

(1): Define label Processing class

Import java.io.IOException;
Import Java.util.Date;
Import Javax.servlet.http.HttpServletRequest;
Import javax.servlet.jsp.JspException;
Import Javax.servlet.jsp.JspWriter;
Import javax.servlet.jsp.tagext.BodyContent;
Import Javax.servlet.jsp.tagext.BodyTagSupport;

public class Bodytag extends Bodytagsupport {

private int count;

Private HttpServletRequest Reqeust;

Private JspWriter out;


public void init () {
Reqeust = (httpservletrequest) pagecontext.getrequest ();
out = Pagecontext.getout ();
}

@Override
public int doStartTag () throws Jspexception {
Init ();
return this. Eval_body_include;
}

Set the current label body
@Override
public void Setbodycontent (Bodycontent bodycontent) {
This.bodycontent = bodycontent;
System.out.println ("Setbodycontent ...");
}

Requires initialization of bodycontent
@Override
public void Doinitbody () throws Jspexception {
System.out.println ("init ...");
}


@Override
public int doafterbody () throws Jspexception {
if (count >= 1) {
try {
Out.println (count);
Out.println ("<Br>");
catch (IOException e) {
E.printstacktrace ();
}
Count--;
return this. Eval_body_again;
} else {
return this. Skip_body;
}
}

@Override
public int Doendtag () throws Jspexception {
Java.text.SimpleDateFormat formater = new Java.text.SimpleDateFormat (
"Yyyy-mm-dd");
String date = Formater.format (new date ());
try {
Out.print (date);
catch (IOException e) {
E.printstacktrace ();
}
return this. Eval_page;
}

Setxx () method must be implemented
public void SetCount (int count) {
This.count = count;
}
}

(2) Defining TLD Files

<?xml version= "1.0" encoding= "UTF-8"
<taglib>
   <tlibversion>1.0</ Tlibversion>
   <jspversion>1.1</jspversion>
  <tag>
     <name>iterator</name>
    <tagclass>cn.com.chenlly.tag.bodytag</ Tagclass>
    <bodycontent>jsp</bodycontent>
    <!--defining Properties-
    <attribute>
       <name>count</name > <!--property name
       <type>int</type>  <!--property Type--
       <requried>false</requried> <!--must be;
        <rtexprvale>false</rtexprvale> <!--Indicate whether you can use the JSP expression  ;
  </ATTRIBUTE>
  </tag>
</taglib>

(3) JSP page dynamic Reference

<%@ page language= "java" pageencoding= "UTF-8"%>
<%@ taglib uri= "/web-inf/bodytag.tld" prefix= "C"%>
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" >
<body>
<c:iterator count= "Ten" >HelloWorld!</c:iterator>
<%
Out.println ("Bye Bye");
%>
</body>

Effect Chart:

Order of execution

doStartTag ()->setbodycontent ()->doinitbody ()->doaftertag ()->doendtag ()
If doStartTag () Returns the Eval_body_include execution Doaftertag () method,
executes the Doendtag () method if it returns Skip_body. The
Setbodycontent () method is used to set the contents of the label body, and if some initialization is required when calculating bodycontent,
It is done in the Doinitbody () method. When the label body content is finished, the Doafterbody () method is invoked to
return Eval_body_again in the Doaftertag () method to repeat the Doaftertag () method
to return to Skip_ The body value executes the Doendtag () method.
The Eval_page value is returned in the Doendtag () method, and other code after this label is executed,
returning Skip_page does not execute other code for this page.

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.