Examples of custom tag usage in jsp and Analysis of jsp instances

Source: Internet
Author: User
Tags key string return tag tld

Examples of custom tag usage in jsp and Analysis of jsp instances

This example describes the use of custom tags in jsp. Share it with you for your reference. The details are as follows:

Here we simply write a custom tag. The advantage of custom tags is that you can use your own defined functions on the jsp page, completely separated from Java code.

1. The tld file is as follows:

First, you need to write the X. tld file. When the project starts with the server, it will check whether the project contains the * tld file.
Write tld 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 "> <! -- Define version --> <tlib-version> 1.0 </tlib-version> <! -- Define the name --> <short-name> apsliyuan </short-name> <! -- Define uri --> <uri> http://my.oschina.net/aps </Uri> <! -- Define your own class --> <tag> <! -- Name is the name of the tag displayed in jsp. <aps: hellowTag/> --> <name> hellowTag </name> <! -- Complete path of the Self-written label class --> <tag-class> cn. itcast. apsliyuan. tag. HellowtTag </tag-class> <! -- Whether the content is to be displayed in the topic in jsp. There are four attributes, for example, empty. On the jsp page, the tag cannot define its own content. Otherwise, the Servlet will be reported. service () for servlet jsp threw exception org. apache. jasper. jasperException:/index. jsp (12, 8) According to TLD, tag aps: hellowTag must be empty, but is not abnormal --> <body-content> JSP </body-content> </tag> <! -- No attribute is written here and there is time to add --> <tag> <name> ApsliyuanTag </name> <tag-class> cn. itcast. apsliyuan. tag. apsliyuanTag </tag-class> <body-content> JSP </body-content> </tag> </taglib>

2. the java code for the custom tag class is as follows:

// Class package for custom tags cn. itcast. apsliyuan. tag; import java. io. IOException; import java. text. simpleDateFormat; import java. util. date; import javax. servlet. jsp. jspException; import javax. servlet. jsp. jspWriter; import javax. servlet. jsp. tagext. tagSupport; public class HellowtTag extends TagSupport {/*****/private static final long serialVersionUID = 1781703371130382609L; @ Override public int doStartTag () throws JspException {// TODO Auto-generated method stub JspWriter out = pageContext. getOut (); SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); try {out. print (format. format (new Date ();} catch (IOException e) {// TODO Auto-generated catch block e. printStackTrace ();} return EVAL_BODY_INCLUDE;} @ Override public int doEndTag () throws JspException {// TODO Auto-generated method stub JspWriter out = pageContext. getOut (); try {out. print ("<br/> 

3. jsp introduces the custom tag Code as follows:

// Introduce custom labels in jsp <% @ page language = "java" contentType = "text/html; charset = UTF-8 "pageEncoding =" UTF-8 "%> <% @ taglib uri =" http://my.oschina.net/aps "prefix =" aps "%> <% @ taglib uri =" http://java.sun.com/jsp/jstl/core "prefix =" c "%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

4. The TagSupport code is as follows:

// Check the source code in TagSupport. This is the adapter mode public class TagSupport implements IterationTag, Serializable {public static final Tag findAncestorWithClass (Tag from, // TCK signature test fails with generics @ SuppressWarnings ("unchecked") Class klass) {boolean isInterface = false; if (from = null | klass = null | (! Tag. class. isAssignableFrom (klass )&&! (IsInterface = klass. isInterface () {return null ;}for (;) {Tag tag = from. getParent (); if (tag = null) {return null;} if (isInterface & klass. isInstance (tag) | klass. isAssignableFrom (tag. getClass () return tag; else from = tag ;}}/*** Default constructor, all subclasses are required to define only * a public constructor with the same signature, and to call the * superclass constru Ctor. ** This constructor is called by the code generated by the JSP * translator. */public TagSupport () {}/ *** Default processing of the start tag, returning SKIP_BODY. ** @ return SKIP_BODY * @ throws JspException if an error occurs while processing this tag ** @ see Tag # doStartTag () */public int doStartTag () throws JspException {return SKIP_BODY ;} /*** Default processing of the end tag Returning EVAL_PAGE. ** @ return EVAL_PAGE * @ throws JspException if an error occurs while processing this tag ** @ see Tag # doEndTag () */public int doEndTag () throws JspException {return EVAL_PAGE ;} /*** Default processing for a body. ** @ return SKIP_BODY * @ throws JspException if an error occurs while processing this tag ** @ see IterationTag # doAfterBody () */public int doAfterBody () throws JspException {return SKIP_BODY;} // Actions related to body evaluation/*** Release state. ** @ see Tag # release () */public void release () {parent = null; id = null; if (values! = Null) {values. clear ();} values = null;}/*** Set the nesting tag of this tag. ** @ param t The parent Tag. * @ see Tag # setParent (Tag) */public void setParent (Tag t) {parent = t;}/*** The Tag instance most closely enclosing this tag instance. * @ see Tag # getParent () ** @ return the parent tag instance or null */public Tag getParent () {return parent;}/*** Set the id attribute for this Tag. ** @ param id The String for the id. */public void setId (String id) {this. id = id;}/*** The value of the id attribute of this tag; or null. ** @ return the value of the id attribute, or null */public String getId () {return id;}/*** Set the page context. ** @ param pageContext The PageContext. * @ see Tag # setPageContext */public void setPageContext (PageContext pageContext) {this. pag EContext = pageContext;}/*** Associate a value with a String key. ** @ param k The key String. * @ param o The value to associate. */public void setValue (String k, Object o) {if (values = null) {values = new Hashtable <String, Object> ();} values. put (k, o);}/*** Get a value associated with a key. ** @ param k The string key. * @ return The value associated with the key, or null. */publ Ic Object getValue (String k) {if (values = null) {return null;} else {return values. get (k) ;}}/*** Remove a value associated with a key. ** @ param k The string key. */public void removeValue (String k) {if (values! = Null) {values. remove (k) ;}/ *** Enumerate the keys for the values kept by this tag handler. ** @ return An enumeration of all the keys for the values set, * or null or an empty Enumeration if no values have been set. */public Enumeration <String> getValues () {if (values = null) {return null;} return values. keys ();} // private fields private Tag parent; private Hashtable <String, Object> values;/*** The value of the id attribute of this tag; or null. */protected String id; // protected fields/*** The PageContext. */protected PageContext pageContext ;}

DoStartTag Return Value

The value returned by doStartTag determines how the data in the body is displayed.

Two return values:

0-SKIP_BODY-constant. The body is not displayed.
1-EVAN_BODY_INCLUDE; contains the body data, which is displayed normally.
3: The doEndTag also has two return values.

Determine whether to display the following page:

SKIP_PAGE: The following page section is no longer displayed.

EVAL_PAGE: displays the following page.

I hope this article will help you with JSP program design.

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.