Small Example of JSP custom tag

Source: Internet
Author: User
Tags tld

I learned some tutorials on JSP custom tags on the Internet and implemented a small example myself.

JSP:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%><%@ taglib uri="/WEB-INF/mytag.tld" prefix="cc"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

Web. xml:

<?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>   <taglib>       <taglib-uri>/WEB-INF/mytag.tld</taglib-uri>       <taglib-location>/WEB-INF/mytag.tld</taglib-location>    </taglib>                 </web-app>

Mytag. TLD:

<? XML version = "1.0" encoding = "UTF-8"?> <Tagliblib> <tlibversion> 1.0 </tlibversion> <jspversion> 1.1 </jspversion> <tag> <Name> displayone </Name> <tagclass> COM. zyujie. util. mytagone </tagclass> <bodycontent> Empty </bodycontent> </Tag> <tag> <Name> displaytwo </Name> <tagclass> COM. zyujie. util. mytagtwo </tagclass> <bodycontent> Empty </bodycontent> <! -- Define Attributes --> <attribute> <Name> pattern </Name> <! -- Attribute name --> <type> string </type> <! -- Property type --> <requried> false </requried> <! -- Required? --> <rtexprvale> false </rtexprvale> <! -- Indicates whether JSP expressions can be used --> </attribute> </Tag> <tag> <Name> iterator </Name> <tagclass> COM. zyujie. util. mytag </tagclass> <bodycontent> JSP </bodycontent> <! -- Define Attributes --> <attribute> <Name> count </Name> <! -- Attribute name --> <type> int </type> <! -- Property type --> <requried> false </requried> <! -- Required? --> <rtexprvale> false </rtexprvale> <! -- Indicates whether JSP expressions can be used --> </attribute> </Tag> </taglib>

Implementation class mytag. Java:

Package COM. zyujie. util; 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 mytag extends bodytagsupport {// execution sequence // dostarttag ()-> setbodycontent ()-> doinitbody ()-> doaftertag ()-> doendt AG () //// if dostarttag () returns eval_body_include, execute the doaftertag () method, // if it returns skip_body, execute the doendtag () method. //// Setbodycontent () method is used to set the content of the label body. If Initialization is required when bodycontent is calculated, /// it is completed in the doinitbody () method. After the label body content is executed, the doafterbody () method will be called // The eval_body_again will be returned in the doaftertag () method to repeatedly execute doaftertag () method //// if the skip_body value is returned, the doendtag () method is executed. //// If the eval_page value is returned in the doendtag () method, other code after the label is executed. //// if the value of skip_page is returned, other code on the page is not executed. Private int count; private httpservletrequest reqeust; private jspwriter out; Public void Init () {reqeust = (httpservletrequest) pagecontext. getrequest (); out = pagecontext. getout () ;}@ overridepublic int dostarttag () throws jspexception {Init (); return this. eval_body_include;} // set the current TAG body @ overridepublic void setbodycontent (bodycontent) {This. bodycontent = bodycontent; system. out. println ("setbodycontent... ");} // You Need to initialize bodycontent @ overridepublic void doinitbody () throws jspexception {system. out. println ("init ..... ") ;}@ overridepublic 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 ;}@overridepublic 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;} // The setxx () method public void setcount (INT count) {This. count = count ;}}

This completes the implementation of custom tags.

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.