API文档:
http://docs.Oracle.com/javaee/7/api/
PRintTextTag.java
package com.lingdu.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.Tag;public class PrintTextTag implements Tag { Tag parent; PageContext pageContext; @Override public int doEndTag() throws JspException { try { //输出一句话 this.pageContext.getOut().append("Hello, I am a tag"); } catch (IOException e) { e.printStackTrace(); } return PrintTextTag.SKIP_BODY; } @Override public int doStartTag() throws JspException { // TODO Auto-generated method stub return 0; } @Override public Tag getParent() { // TODO Auto-generated method stub return null; } @Override public void release() { // TODO Auto-generated method stub } @Override public void setPageContext(PageContext arg0) { this.pageContext = arg0; } @Override public void setParent(Tag arg0) { this.parent = arg0; }}在WEB-INF中新建一个pt.tld文件
写入配置文件
<?xml version="1.0" encoding="UTF-8"?><taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <!-- 定义标签的名字 --> <name>hi</name> <!-- 引用的类 --> <tag-class>com.lingdu.tag.PrintTextTag</tag-class> <body-content>empty</body-content> </tag></taglib>引用pt.tld配置文件:
<%@ taglib prefix="pt" uri="/WEB-INF/pt.tld"%>pt.jsp源码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%><%@ taglib prefix="pt" uri="/WEB-INF/pt.tld"%><!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>Insert title here</title></head><body><pt:hi/></body></html>PrintTextAttr.java
package com.lingdu.tag;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.Tag;public class PrintTextAttr implements Tag { Tag parent; PageContext pageContext; String username; @Override public int doEndTag() throws JspException { try { this.pageContext.getOut().append("带body的标签"); if(this.getUsername().equals("lingdu")){ this.pageContext.getOut().append("lingdu"); }else{ this.pageContext.getOut().append("You don't enter lingdu !"); } } catch (IOException e) { e.printStackTrace(); } return this.SKIP_BODY; } public String getUsername(){ return username; } public void setUsername(String username){ this.username = username; } @Override public int doStartTag() throws JspException { // TODO Auto-generated method stub return 0; } @Override public Tag getParent() { // TODO Auto-generated method stub return null; } @Override public void release() { // TODO Auto-generated method stub } @Override public void setPageContext(PageContext arg0) { this.pageContext = arg0; } @Override public void setParent(Tag arg0) { this.parent = arg0; }}在WEB-INF中新建一个pt.tld文件
写入配置文件
<?xml version="1.0" encoding="UTF-8"?><taglib version="2.0" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee web-jsptaglibrary_2_0.xsd"> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD2</short-name> <tag> <!-- 定义标签名字 --> <name>pt2</name> <!-- 指定类的路径 --> <tag-class>com.lingdu.tag.PrintTextAttr</tag-class> <body-content>empty</body-content> <attribute> <!-- 自定义定义属性 --> <name>username</name> <!-- 是否是必须属性 --> <required>false</required> <!-- 使用标签时是否可以使用表达式来动态指定数据 --> <rtexprvalue>true</rtexprvalue> </attribute> </tag></taglib>引用pt.tld配置文件:
<%@ taglib prefix="pt" uri="/WEB-INF/pt.tld"%>pt2.jsp源码:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="pt2" uri="/WEB-INF/pt2.tld"%><!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>Insert title here</title></head><body><% //测试属性 request.setAttribute("username", "lingdu");%><pt2:pt2 username="${username }"/></body></html>将测试属性去掉
每个tag元素在tag库中都要指出它的名字、类名、脚本变量、tag的属性。其中脚本变量的值可以直接在TLD中定义或通过tag附加信息的类来取得。每个属性描述了这个属性是否可以省略,它的值是否可以通过<%=…%>这样的JSP语法来获得,以及属性的类型。每一个tag在TLD中对应一个tag元素,下表是tag元素的子元素:
name: //独一无二的元素名tag-class: //Tag标签对应的tag处理类tei-class: //javax.servlet.jsp.tagext.TagExtraInfo的子类,用于表达脚本变量(可选)body-content: //Tag标签body的类型display-name: //被可视化工具用来显示的名称(可选)small-icon: //被可视化工具用来显示的小图标(可选)large-icon: //被可视化工具用来显示的大图标(可选)description: //此tag标签的描述variable: //提供脚本变量的信息 (可选)attribute: //Tag标签的属性名
新闻热点
疑难解答