在mvc模式中,我们可以使用别人提供的标签,例如c标签,就是比较常用的,不过,如果别人提供的标签不适用我们的项目开发了,那么我们也可以自己根据不同需求定义标签,也就是自定义标签。
1.标签语言的特点
例如a标签:<a>bbb</a>
<a>就是开始标签
bbb就是标签体
</a>就是结束标签
还有一些不同的,没有标签体或者结束标签,例如<br><hr>
2.自定义标签的步骤
例如a标签:<a>bbb</a>
<a>就是开始标签
bbb就是标签体
</a>就是结束标签
还有一些不同的,没有标签体或者结束标签,例如<br><hr>
2.自定义标签的步骤
3.标签的生命周期
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<!-- 标签库描述符 -->
<taglib xmlns="http://java.sun.com/JSP/TagLibraryDescriptor">
<!-- 代表标签库的版本号 -->
<tlib-version>1.0</tlib-version>
<!-- 代表jsp的版本 -->
<jsp-version>1.2</jsp-version>
<!-- 你的标签库的简称 -->
<short-name>test</short-name>
<!-- 你标签库的引用uri -->
<uri>/zking</uri>
<tag>
<!-- 标签名 -->
<name>test</name>
<!-- 标签工具类 -->
<tag-class>com.zking.tan.Test</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>jsp</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>name</name>
<!-- true表示必填 -->
<required>true</required>
<!-- true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- 标签名 -->
<name>forEach</name>
<!-- 标签工具类 -->
<tag-class>com.zking.tan.ForEachTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>jsp</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<!-- <attribute>
自定义标签的属性名称
<name>name</name>
true表示必填
<required>true</required>
true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持
<rtexprvalue>false</rtexprvalue>
</attribute> -->
</tag>
<tag>
<!-- 标签名 -->
<name>forEach</name>
<!-- 标签工具类 -->
<tag-class>com.zking.helper.ForEachTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>jsp</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<!-- <attribute>
自定义标签的属性名称
<name>name</name>
true表示必填
<required>true</required>
true支持动态值,可以向值里面填jsp表达式、EL表达式,false则不支持
<rtexprvalue>false</rtexprvalue>
</attribute> -->
</tag>
<tag>
<!-- 标签名 -->
<name>if</name>
<!-- 标签工具类 -->
<tag-class>com.zking.helper.IfTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>jsp</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- 标签名 -->
<name>out</name>
<!-- 标签工具类 -->
<tag-class>com.zking.helper.OutTag</tag-class>
<!-- 标签的内容类型:empty表示空标签,jsp表示可以为任何合法的JSP元素 -->
<body-content>jsp</body-content>
<!-- 自定义标签的属性定义,请注意一定要在标签类中提供对应的get/set方法 -->
<attribute>
<!-- 自定义标签的属性名称 -->
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="/zking" %>
<!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>
<%
String [] students ={"aa","bb","cc"};
request.setAttribute("students", students);
%>
<z:if test="${empty students}">
我要去干饭
</z:if>
<z:forEach>
这么快就去干饭吗?
</z:forEach>
<z:out value="你妈炸了?">输出</z:out>
</body>
</html>
out标签
package com.zking.helper;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
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 {
// TODO Auto-generated method stub
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return super.doStartTag();
}
}
2.foreach
package com.zking.helper;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForEachTag extends BodyTagSupport {
@Override
public int doAfterBody() throws JspException {
int count =(int)pageContext.getAttribute("count");
if(count<3) {
count++;
pageContext.setAttribute("count", count);
return EVAL_BODY_AGAIN;
}
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
// TODO Auto-generated method stub
return super.doEndTag();
}
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute("count", 1);
return EVAL_BODY_INCLUDE;
}
}
3.if 控制标签
package com.zking.helper;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport {
private Boolean test;
public Boolean getTest() {
return test;
}
public void setTest(Boolean test) {
this.test = test;
}
@Override
public int doAfterBody() throws JspException {
return super.doAfterBody();
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
@Override
public int doStartTag() throws JspException {
return test?EVAL_BODY_INCLUDE:SKIP_PAGE;
}
}