一个循环标签:对标签体内容进行循环三次
自定义标签的编写:
public class MyIteration extends TagSupport {
private int num;//执行次数 3
public void setNum(int num){
this.num = num;
}
//执行标签
@Override
public int doStartTag() throws JspException {
return EVAL_BODY_INCLUDE;//先执行一次
}
//执行完毕后
@Override
public int doAfterBody() throws JspException {
num--;
return num > 0 ? EVAL_BODY_AGAIN : SKIP_BODY;//重复执行
}
}
继承TagSupport,利用TagSupport中的doStart方法和doAfterBody方法进行,对标签的控制。
1、doStart方法的返回值为EVAL_BODY_INCLUDE,为执行标签内容。
2、 doAfterBody方法的返回值利用三目运算,判断num的值,返回值为EVAL_BODY_AGAIN即重复在执行,返回值为SKIP_BODY即为结束标签体。
对;编写的标签进行配置:
<?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>我的自定义迭代器标签</description>
<tlib-version>1.0</tlib-version>
<short-name>MyTag</short-name>
<uri>http://www.wpc.com</uri>
<!-- 自定义标签的相关信息 -->
<tag>
<description>我的自定义迭代器标签</description>
<name>myTag</name>
<tag-class>org.wpc.tag.MyIteration</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>num</name>
<required>true</required>
</attribute>
</tag>
</taglib>
以上内容中:
<description>我的自定义迭代器标签</description>
<tlib-version>1.0</tlib-version>
<short-name>MyTag</short-name>
<uri>http://www.wpc.com</uri>
以上代码是对自定义标签的配置文件的一个描述:
```handlebars
<description>对标签内容的描述</description>
<tlib-version>标签的版本信息</tlib-version>
<short-name>标签文件名</short-name>
<uri>对标签文件的引用url</uri>
对编写的标签的描述:
<tag>
<description>标签的描述</description>
<name>标签名</name>
<tag-class>引用的类</tag-class>
<body-content>标签体类型(JSP、scriptless)</body-content>
<attribute>
<name>传入的属性名</name>
<required>对属性的强制要求(true、false)</required>
</attribute>
</tag>
标签的配置文件后缀为.tld。如图:

自定义标签的使用:
<%@ taglib prefix="wpc" uri="http://www.wpc.com" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<wpc:myTag num="3">hello</wpc:myTag>
</body>
</html>
对标签体的引入:
<%@ taglib prefix="wpc" uri="http://www.wpc.com" %>
对标签体的使用:
<wpc:myTag num="3">hello</wpc:myTag>

被折叠的 条评论
为什么被折叠?



