使用简单标签 很简单,分为以下步骤:
1. 创建一个类 继承SimpleTagSupport这个类 重写 他的doTag方法 其中 dotag方法中最重要的是 JspFragment jf = this.getJspBody(); 这个获取标签体 然后进行一系列操作
2. 创建一个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>A tag library exercising SimpleTag handlers.</description>
<tlib-version>1.0</tlib-version>
<short-name>ys</short-name>
<uri>http://yangsheng.cn</uri>
<tag>
<name>ViewIp</name>
<tag-class>cn.web.tag.Tag1</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
<name>demo1</name>
<tag-class>simpleTag.Demo1</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>demo2</name>
<tag-class>simpleTag.Demo2</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>demo3</name>
<tag-class>simpleTag.Demo3</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>demo4</name>
<tag-class>simpleTag.Demo4</tag-class>
<body-content>empty</body-content>
</tag>
<tag>
<name>demo5</name>
<tag-class>simpleTag.Demo5</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>date</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
类似于这个样子的
3. 在jsp中使用标签:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://yangsheng.cn" prefix="ys" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%--<ys:demo4/> --%>
<html>
<head>
</head>
<body>
输出内容: <ys:demo1>
aaaaa
</ys:demo1>
<hr>
循环输出: <ys:demo2>
bbbbb
</ys:demo2>
<hr>
修改输出:<ys:demo3>
ccccc
</ys:demo3>
<hr>
带参数的标签:<ys:demo5 count="10" date="<%=new Date() %>">
dddd
</ys:demo5>
</body>
</html>
1. 使用标签实现输出文本内容
JspFragment jf = this.getJspBody();
jf.invoke(null); //默认输出标签体
2.循环输出
JspFragment jf = this.getJspBody();
for (int i = 0; i < 4; i++) {
jf.invoke(null);
}
3.修改输出
JspFragment jf = this.getJspBody();
StringWriter sw = new StringWriter();
jf.invoke(sw);
String content = sw.toString();
this.getJspContext().getOut().print(content+"aaaaa");
4.控制下面内容不输出
throw new SkipPageException();
private int count;
private Date date;
public void setCount(int count) {
this.count = count;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
this.getJspContext().getOut().write(date.toLocaleString());
for(int i = 0;i<count;i++){
jf.invoke(null);
}
}
<tag>
<name>demo5</name>
<tag-class>simpleTag.Demo5</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>count</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>date</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
name--标签名 required--属性是否必须 rtexprvalue--是否可用EL表达式 和<%%>代码块 type--属性的类型(可不写)