jsp自定义标签
注:
1、标签库的运作离不开tld文件
2、标签库的标签是定义在tld中的tag标签内(助手类)
标签语言特点
<开始标签 属性="属性值">标签体</结束标签>
空标签
<br/><hr/>
<开始标签></结束标签>
<开始标签/>
ui标签
控制标签
数据标签
自定义标签的开发及使用步骤
1.创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法rtexprvalue
package com.wupeng.jsp.day01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author wupeng
*
*/
public class DemoTag extends BodyTagSupport {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
private static final long serialVersionUID = -7666667734848027555L;
@Override
public int doStartTag() throws JspException {
System.out.println("---------doStartTag------------");
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("---------doAfterBody------------");
return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("---------doEndTag------------");
return super.doEndTag();
}
}
创建标签库描述文件(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>/wupeng</uri>
<tag>
<!-- 标签库中的标签(类似c:set c:out的定义) -->
<name>Demo</name>
<!-- 是标签运行具体代码,也就是助手类,下面填写的助手类的全路径 -->
<tag-class>com.wupeng.jsp.day01.DemoTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 该标签的属性 -->
<name>test</name>
<!-- 该属性是否必填 -->
<required>true</required>
<!--是否支持表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/wupeng" 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<c:set value="zhangsan" var="name"></c:set>
-------------
<c:out value="${name}"></c:out>
<z:Demo test="zhangsan">xxxx</z:Demo>
</body>
</html>
自定义标签:
<%@ taglib uri="/wupeng" prefix="z"%>
标签生命周期
jsp自定义标签生命周期图
1.实例化标签助手类->doStartTag()------------->doEndTag()主要用开发简单标签
2.实例化标签助手类->doStartTag()------------->doAfterBody---------------->doEndTag()
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
1.如果默认情况,如果jsp上面有标签体那么3个方法都会执行
package com.wupeng.jsp.day01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author wupeng
*
*/
public class DemoTag extends BodyTagSupport {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
private static final long serialVersionUID = -7666667734848027555L;
@Override
public int doStartTag() throws JspException {
System.out.println("---------doStartTag------------");
return super.doStartTag();
}
@Override
public int doAfterBody() throws JspException {
System.out.println("---------doAfterBody------------");
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
System.out.println("---------doEndTag------------");
return super.doEndTag();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/wupeng" 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<z:Demo test="zhangsan">xxxx</z:Demo>
</body>
</html>
结果:
2.如果将doStartTag的默认返回值改成SKIP_BODY就不会执行doAfterBody(即使有标签体也不例外)
package com.wupeng.jsp.day01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author wupeng
*
*/
public class DemoTag extends BodyTagSupport {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
private static final long serialVersionUID = -7666667734848027555L;
@Override
public int doStartTag() throws JspException {
System.out.println("---------doStartTag------------");
return SKIP_BODY;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("---------doAfterBody------------");
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
System.out.println("---------doEndTag------------");
return super.doEndTag();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/wupeng" 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<z:Demo test="zhangsan">xxxx</z:Demo>
</body>
</html>
结果:
3.如果将doStartTag,doAfterBody返回值改变成EVAL_BODY_INCLUDE,EVAL_BODY_AGAIN就会无限循环标签体
package com.wupeng.jsp.day01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* Demo标签的助手类
* @author wupeng
*
*/
public class DemoTag extends BodyTagSupport {
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
private static final long serialVersionUID = -7666667734848027555L;
@Override
public int doStartTag() throws JspException {
System.out.println("---------doStartTag------------");
return EVAL_BODY_INCLUDE;
}
@Override
public int doAfterBody() throws JspException {
System.out.println("---------doAfterBody------------");
return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("---------doEndTag------------");
return super.doEndTag();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="/wupeng" 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=UTF-8">
<title>Insert title here</title>
</head>
<body>
<z:Demo test="zhangsan">标签体</z:Demo>
</body>
</html>
结果: