标签库的运作离不开tld文件
标签库的标签是定义在tld中的tag标签内(助手类)
1. 标签语言特点
<开始标签 属性="属性值">标签体</结束标签>
空标签
<br/><hr/>
<开始标签></结束标签>
<开始标签/>
ui标签
控制标签
数据标签
1. 自定义标签的开发及使用步骤
1.1 创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法
rtexprvalue
package com.houyitao.jsp01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author hou
*
*/
public class DemoTag extends BodyTagSupport{
private static final long serialVersionUID = -6367842071288835719L;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doStartTag-------");
return super.doStartTag();
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doAfterBody-------");
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doEndTag-------");
return super.doEndTag();
}
}
1.2 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
<?xml version="1.0" encoding="UTF-8" ?>
<taglib 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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/houyitao</uri>
<tag>
<!-- 标签库中的标签(类似c:set c:out的定义) -->
<name>demo</name>
<!-- 是标签运行具体的代码,也就是助手类,下面填写的是助手类的全路径名 -->
<tag-class>com.houyitao.jsp01.DemoTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 该标签的属性 -->
<name>test</name>
<!-- 该标签的是否必填 -->
<required>true</required>
<!-- 该标签的是否支持表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
1.3 在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<%@ taglib uri="/houyitao" prefix="z" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<z:demo test="无比">123</z:demo>
</body>
</html>
2. 标签生命周期
SKIP_BODY
2.1 实例化标签助手类->doStartTag()------------->doEndTag()
//主要用开发简单标签
EVAL_BODY_INCLUDE SKIP_BODY
2.2 实例化标签助手类->doStartTag()------------->doAfterBody---------------->doEndTag()…
EVAL_BODY_AGAIN
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
3.如果将doStartTag的默认返回值改成SKIP_BODY就不会执行doAfterBody(即使有标签体也不例外)
package com.houyitao.jsp01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author hou
*
*/
public class DemoTag extends BodyTagSupport{
private static final long serialVersionUID = -6367842071288835719L;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doStartTag-------");
return SKIP_BODY;
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doAfterBody-------");
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doEndTag-------");
return super.doEndTag();
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<%@ taglib uri="/houyitao" prefix="z" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<z:demo test="无比">123</z:demo>
</body>
</html>
4.如果将doStartTag,doAfterBody返回值改变成EVAL_BODY_INCLUDE,EVAL_BODY_AGAIN就会无限循环标签体
package com.houyitao.jsp01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author hou
*
*/
public class DemoTag extends BodyTagSupport{
private static final long serialVersionUID = -6367842071288835719L;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doStartTag-------");
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doAfterBody-------");
return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
System.out.println("------doEndTag-------");
return super.doEndTag();
}
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="utf-8"%>
<%@ taglib uri="/houyitao" prefix="z" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<z:demo test="无比">123</z:demo>
</body>
</html>