Java learning notes-custom tag 1 (39), java learning notes

Source: Internet
Author: User

Java learning notes-custom tag 1 (39), java learning notes

In actual development, for example, to simplify JSP scripts, we need to use the standard tag library and EL expressions, but the labels provided in the new tag library are limited, it is impossible to fully meet the development needs. For example, pagination. Therefore, you need to learn how to customize your own tag library.

Public class HelloHanler implements Tag {private PageContext pageContext = null; // when the Tag ends, execute public int doEndTag () throws JspException {return 0 ;} // execute public int doStartTag () throws JspException when the tag starts {// Output A hello Message to the page. JspWriter out = pageContext. getOut (); // output String info = "hello custom tag"; try {out. write (info);} catch (IOException e) {e. printStackTrace ();} return 0;} // obtain its parent Tag public Tag getParent () {return null;} // release public void release () {}// set the jsp context object public void setPageContext (PageContext pc) {this. pageContext = pc;} // set the parent Tag public void setParent (Tag t ){}}

2. description file

<? Xml version = "1.0" encoding = "UTF-8"?> <Taglib 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-jsptaglibrary_2_1.xsd" version = "2.1"> <! -- 2. compile the tag library description file --> <tlib-version> 1.0 </tlib-version> <short-name> jnb </short-name> <tag> <name> hello </ name> <tag-class> cn. itcast. test. helloHanler </tag-class> <body-content> empty </body-content> </tag> </taglib>

3. Introduction

<%@taglib uri="/WEB-INF/test.tld" prefix="jnb"%>     <br/><jnb:hello/>
JSP1.2 for custom tag Development

Customize a tag for a real date.

1. Implement tag processing class that can process tag attributes

Public class ShowDate extends TagSupport {// to facilitate the acquisition of attributes, you can define the attribute variables with the same name as the attributes in the processing class and provide the get and set Methods private String pattern; public String getPattern () {return pattern;} public void setPattern (String pattern) {this. pattern = pattern;} // automatically execute public int doStartTag () throws JspException when the tag starts {// create Date object date = new Date (); // create the formatting object SimpleDateFormat format = new SimpleDateFormat (getPattern (); // format String str = format. format (date); // obtain the JSP context object PageContext pageContext = this. pageContext; // get the OUT output stream of JSP JspWriter out = pageContext. getOut (); // output try {out. write (str);} catch (IOException e) {e. printStackTrace ();} return super. doStartTag ();}}

2. description file

<Taglib label library description file root element 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-jsptaglibrary_2_1.xsd" version = "2.1"> <! -- 2. compile the tag library description file --> <tlib-version> 1.0 </tlib-version> specify the version of the tag Library (required) <short-name> jnb </short-name> specifies the short name (required) of the tag library) <tag> specify a tag start <name> showdate </name> tag name <tag-class> cn. itcast. custom. showDate </tag-class> specifies the tag processing class <body-content> empty </body-content> specifies the tag body, JSP (with) empty () <attribute> description attribute <name> pattern </name> description of the attribute name <required> true </required> <rtexprvalue> true </rtexprvalue> attribute value </attribute> </tag> </taglib>

3. Introduction and use

<% @ Taglib uri = "/WEB-INF/date. tld" prefix = "date" %> <date: showdate pattern = "yyyy MM dd a E"/>

 

Implement custom tags with tags

1. Tag Processing

Public class ShowDateByBody extends BodyTagSupport {// to facilitate the acquisition of attributes, you can define the attribute variables with the same name as the attributes in the processing class and provide the get and set Methods private String pattern; public String getPattern () {return pattern;} public void setPattern (String pattern) {this. pattern = pattern;} // automatically execute public int doStartTag () throws JspException when the tag starts {// create Date object date = new Date (); // create the formatting object SimpleDateFormat format = new SimpleDateFormat (getPattern (); // format String str = format. format (date); // obtain the JSP context object PageContext pageContext = this. pageContext; // get the OUT output stream of JSP JspWriter out = pageContext. getOut (); // get the BodyContent body = this. getBodyContent (); String tag_body = body. getString (); str = "<font color = 'red'>" + tag_body + "</font>" + str; // output try {out. write (str);} catch (IOException e) {e. printStackTrace ();} return super. doStartTag ();}}

2. description file

  <tag>      <name>showdate2</name>    <tag-class>cn.itcast.custom.ShowDateByBody</tag-class>    <body-content>JSP</body-content>    <attribute>        <name>pattern</name>        <required>true</required>        <rtexprvalue>true</rtexprvalue>    </attribute>  </tag>

3. Introduction and use

<Date: showdate2 pattern = "yyyy-MM-dd"> system time: </date: showdate2>

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.