目录
一、标签语言特点
形式:<开始标签 属性=“属性值”>标签体</结束标签>
分类:
空标签 有br 、hr
ui标签有 intput、table
控制标签 有if、foreach
数据标签 有out 标签
二、自定义标签的开发及使用步骤
步骤:1.创建助手类(继承BodytagSupport)
package com.lucy.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;/**助手类 必须继承BodyTagSupport
* @author lucy
*
*/
public class DemoTag1 extends BodyTagSupport{
@Override
public int doStartTag() throws JspException {
System.out.println("========doStartTag=======");
// return SKIP_BODY ;
return EVAL_BODY_INCLUDE ;
// return super.doStartTag();
}
@Override
public int doAfterBody() throws JspException {
System.out.println("==========doAfterBody=========");
return super.doAfterBody();
// return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("===========doEndTag===========");
// return super.doEndTag();
return SKIP_PAGE;
}
}
2.标签库描述文件(.tld)
tld文件必须保存到web-inf目录或者字目录
3.jsp通过taglib指令导入标签库
实例:c:if 换成zif
package com.lucy.jsp;---------------------------------助手类
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;/**
* if标签:
* 分析:如果满足条件,就打印标签体->doStartTag的返回值eval_body_include
* 如果不满足条件,就不输出标签体--->doStartTag的返回值skip_body
* 需要获取到满足条件的结果值,那么该标签就有一个属性,属性值是boolean
*
* @author lucy
*
*/
public class Iftag extends BodyTagSupport {
private boolean test;
public boolean isTest() {
return test;
}public void setTest(boolean test) {
this.test = test;
}@Override
public int doStartTag() throws JspException {
return test ? EVAL_BODY_INCLUDE: SKIP_BODY;
}
}
--------------------------------------------------------------tld类
<?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>z</short-name>
<uri>http://jsp.veryedu.cn</uri><validator>
<description>
Provides core validation features for JSTL tags.
</description>
<validator-class>
org.apache.taglibs.standard.tlv.JstlCoreTLV
</validator-class>
</validator>
<tag>
<name>if</name>
<tag-class>com.lucy.jsp.Iftag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 代表自定义jsp标签属性名 -->
<name>test</name>
<!-- 代表属性是否必填 -->
<required>true</required>
<!-- 该属性是否支持Element表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
-----------------------------------------------------------------------jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" 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>
<!--
1.在有标签的情况下,会默认调用助手类的doStartBody,doAfterbody,doeEndTag方法
2.如果将dostarttag的返回值改为skip_body,那么doafterbody就不会调用执行
3.如果将dostarttag的返回值改为eval_body_include,那么doafterbody就会执行
4.如果将dostarttag的返回值改为eval_body_again,那么就会一直调用doafterbody,就会进入循环打印doafterbody
-->
<z:if test="true">true</z:if>
<z:if test="false">false</z:if>
</body>
</html>
三、标签生命周期
返回值:EVAL_BODY_INCLUDE-----计算标签主体内容并且输出
EVAL_BODY_AGAIN-----------再计算主体一次
EVAL_PAGE----------计算后续部分
SKIP_PAGE-------跳过后续部分
SKIP_BODY -----跳过主体
标签开发场景代码如下:
1.在有标签的情况下,会默认调用助手类的doStartBody,doAfterbody,doeEndTag方法
2.如果将dostarttag的返回值改为skip_body,那么doafterbody就不会调用执行
3.如果将dostarttag的返回值改为eval_body_include,那么doafterbody就会执行
4.如果将dostarttag的返回值改为eval_body_again,那么就会一直调用doafterbody,就会进入循环打印doafterbody
package com.lucy.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**助手类 必须继承BodyTagSupport
* @author lucy
*
*/
public class DemoTag1 extends BodyTagSupport{
@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 super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
System.out.println("===========doEndTag===========");
return SKIP_PAGE;
}
}
效果图如下:
四、z:if标签
【tld类】
<tag>
<name>if</name>
<tag-class>com.lucy.jsp.Iftag</tag-class>
<body-content>JSP</body-content>
<attribute>
<!-- 代表自定义jsp标签属性名 -->
<name>test</name>
<!-- 代表属性是否必填 -->
<required>true</required>
<!-- 该属性是否支持Element表达式 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>================================================================
【IfTag类】
public class Iftag extends BodyTagSupport {
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}@Override
public int doStartTag() throws JspException {
return test ? EVAL_BODY_INCLUDE: SKIP_BODY;
}
}==================================================================
【Demo界面类】
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" 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:Demo1>xx</z:Demo1>
<z:if test="true">true</z:if>
<z:if test="false">false</z:if>
<z:set var="name" value="laoliu"></z:set>
<z:out value="${name }"></z:out>
</body>
</html>
五、z:set与z:out标签
z:set与z:out的运用:pageContext.setA
=================================SetTag 类
package com.lucy.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;/**数据标签:存储数据
* 作用:pageContext,request,session,application
* @author lucy
*
*/
public class SetTag extends BodyTagSupport{
private String var;
private Object value;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
return super.doStartTag();
}
=================================OutTag类
package com.lucy.jsp;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;/**
* servlet中后台代码前台输出内容,out.print
* 将数据输出前台,首先拿到输出流
*
* @author lucy
*
*/
public class OutTag extends BodyTagSupport{
private Object value;public Object getValue() {
return value;
}public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
}
=========================tld类
<tag>
<name>set</name>
<tag-class>com.lucy.jsp.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.lucy.jsp.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" 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:if test="true">true</z:if>
<z:if test="false">false</z:if>
<z:set var="name" value="laoliu"></z:set>
<z:out value="${name }"></z:out>
</body>
</html>
package com.lucy.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;/**助手类 必须继承BodyTagSupport
* @author lucy
*
*/
public class DemoTag1 extends BodyTagSupport{
@Override
public int doStartTag() throws JspException {
System.out.println("========doStartTag=======");
return SKIP_BODY;
// return EVAL_BODY_INCLUDE ;
// return super.doStartTag();
}
@Override
public int doAfterBody() throws JspException {
System.out.println("==========doAfterBody=========");
return super.doAfterBody();
// return EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("===========doEndTag===========");
return super.doEndTag();
// return SKIP_PAGE;
}
}
效果图如下: