本文實例講述了jsp中自定義標簽用法。分享給大家供大家參考。具體如下:
這里簡單的寫了一個自定義標簽,自己定義標簽的好處就是在jsp頁面中可以使用自己定義的功能,完全與Java代碼分離
1. tld文件如下:
首先是要寫×.tld文件,當項目隨著服務器啟動的時候,會檢查項目中有沒有*tld文件。
寫的tld文件
<?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"> <!-- 定義版本 --> <tlib-version>1.0</tlib-version> <!-- 定義名字 --> <short-name>apsliyuan</short-name> <!-- 定義uri --> <uri>http://my.oschina.net/aps</uri> <!-- 定義自己的類 --> <tag> <!-- name 就是在jsp中顯示的標簽名字,<aps:hellowTag/> --> <name>hellowTag</name> <!-- 自己寫的標簽類的完整路徑 --> <tag-class>cn.itcast.apsliyuan.tag.HellowtTag</tag-class> <!-- jsp中主題中是不是要顯示內容,有四個屬性, 如果為empty的話。在jsp頁面中標簽就不能定義自己的內容了,否則會報 Servlet.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 異常 --> <body-content>JSP</body-content> </tag><!-- 這里沒有寫屬性,有時間補上 --> <tag> <name>ApsliyuanTag</name> <tag-class>cn.itcast.apsliyuan.tag.ApsliyuanTag</tag-class> <body-content>JSP</body-content> </tag></taglib>
2. 自定義標簽類java代碼如下:
//自定義標簽的類package 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/> <hr/>標簽執行完畢了"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return EVAL_PAGE; }}
3. jsp引入自己定義標簽代碼如下:
//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"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>index.jsp</title></head><body> 現在的時間是:<aps:hellowTag> </aps:hellowTag><br/> <hr/> 我愛的人是: <aps:ApsliyuanTag/></body></html>
4. TagSupport代碼如下:
//看看TagSupport中的源代碼, 這個是適配器模式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 constructor. * * 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.pageContext = 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 the value associated with a key. * * @param k The string key. * @return The value associated with the key, or null. */ public 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的返回值
在doStartTag返回的值決定的body部分的數據如何顯示。
兩個返回值:
中文字幕日韩在线观看| 国模精品一区二区三区色天香| 国产午夜精品全部视频在线播放| 欧美成人手机在线| 秋霞成人午夜鲁丝一区二区三区| 一区二区三区高清国产| 精品国内自产拍在线观看| 久久精品99久久久久久久久| 国产精品高清网站| 久久免费少妇高潮久久精品99| 国产成人精品优优av| 北条麻妃一区二区三区中文字幕| 一本一本久久a久久精品综合小说| 久久网福利资源网站| 国产日韩欧美在线看| 色综合天天狠天天透天天伊人| 激情成人在线视频| 粗暴蹂躏中文一区二区三区| 亚洲福利在线观看| 91夜夜未满十八勿入爽爽影院| 国产亚洲免费的视频看| 国产视频久久久| 午夜精品美女自拍福到在线| 国产精品成久久久久三级| 日韩av在线精品| 精品亚洲男同gayvideo网站| 久久伊人免费视频| 欧美日韩亚洲精品内裤| 国产精品美女久久久久久免费| 欧美怡红院视频一区二区三区| 欧美大片欧美激情性色a∨久久| 91po在线观看91精品国产性色| 92福利视频午夜1000合集在线观看| 国产精品户外野外| 国产视频亚洲精品| 精品日本高清在线播放| 国产亚洲日本欧美韩国| 久久久爽爽爽美女图片| 国产精品一区二区久久久久| 日韩欧美国产高清91| 久久久女女女女999久久| 日韩在线视频国产| 欧美高跟鞋交xxxxhd| 欧美黄色性视频| 92看片淫黄大片看国产片| 亚洲精品美女久久久| 日韩一级黄色av| 精品女厕一区二区三区| 久久久久久国产免费| 日本精品久久久久久久| 国产成人精品亚洲精品| 日韩中文字幕不卡视频| 亚洲人成欧美中文字幕| 亚洲精品成人网| 亚洲精品视频在线播放| 亚洲国产精品一区二区久| 久久999免费视频| 亚洲精品视频网上网址在线观看| 久久久精品国产| 黑人巨大精品欧美一区二区一视频| 亚洲国产小视频| 国产女人18毛片水18精品| 久久久久久久久久亚洲| 中文字幕在线观看日韩| 国产精品第三页| 欧美在线视频免费| 日韩欧美国产网站| 国产91av在线| 久久久精品2019中文字幕神马| 亚洲欧美国产va在线影院| 尤物yw午夜国产精品视频| 日韩在线观看免费av| 国产99视频精品免视看7| 亚洲日本欧美中文幕| 国产精品av免费在线观看| 欧美精品久久久久久久久久| 国产精品一区二区三区毛片淫片| 欧美国产亚洲视频| 国产91精品不卡视频| 欧美日韩黄色大片| 欧美高清视频免费观看| 草民午夜欧美限制a级福利片| 欧美洲成人男女午夜视频| 欧美激情亚洲综合一区| 九色精品免费永久在线| 亚洲欧美日韩国产精品| 亚洲aⅴ男人的天堂在线观看| 精品国偷自产在线视频99| 中文字幕成人精品久久不卡| 国产精品jizz在线观看麻豆| 成人网在线观看| 亚洲精品720p| 久久久亚洲精选| 日本一区二三区好的精华液| 97视频在线观看播放| 色综合伊人色综合网站| 欧美国产日韩一区二区在线观看| 狠狠综合久久av一区二区小说| 久久综合网hezyo| 亚洲精品国产综合久久| 91麻豆桃色免费看| 欧美另类69精品久久久久9999| 欧美日韩亚洲精品内裤| 91久久综合亚洲鲁鲁五月天| 美女扒开尿口让男人操亚洲视频网站| 一区二区三区天堂av| 欧美夫妻性生活视频| 亲子乱一区二区三区电影| 亚洲国产婷婷香蕉久久久久久| 成人欧美一区二区三区黑人| 久久久噜噜噜久噜久久| 国产ts人妖一区二区三区| 久久伊人精品一区二区三区| 欧美精品一区二区免费| 韩日欧美一区二区| 亚洲男人天堂2024| 亚洲网址你懂得| 成人乱色短篇合集| 自拍偷拍亚洲区| 高清欧美性猛交| 中国人与牲禽动交精品| 中文字幕亚洲自拍| 成人444kkkk在线观看| 国产又爽又黄的激情精品视频| 成人免费在线视频网站| 国产成+人+综合+亚洲欧洲| 国产欧美日韩精品丝袜高跟鞋| 在线观看欧美日韩国产| 欧美激情免费在线| 国产精品久久久久91| 亚洲第一网站免费视频| 91在线直播亚洲| 伊人久久综合97精品| 欧美电影院免费观看| 欧美成aaa人片在线观看蜜臀| 日韩在线视频免费观看| 亚洲春色另类小说| 欧美日韩亚洲国产一区| 色婷婷综合成人av| 亚洲免费电影一区| 美日韩精品视频免费看| 在线视频一区二区| 精品日韩美女的视频高清| 51视频国产精品一区二区| 久久久久九九九九| 欧美精品在线第一页| 都市激情亚洲色图| 国产精品成人va在线观看| 欧美性生活大片免费观看网址| 国产日韩欧美一二三区| 欧美日韩成人在线观看| 亚洲精品第一国产综合精品| 日韩www在线| 欧美黄色成人网| 国产精品久久久久7777婷婷| 黑人与娇小精品av专区| 国产日韩欧美成人| 欧美xxxx做受欧美| 欧美日韩一区二区免费视频| 在线国产精品视频| 欧美一级视频在线观看| 国产噜噜噜噜噜久久久久久久久| 午夜剧场成人观在线视频免费观看| 国产精品美女久久久久av超清|