JSP2.0的自定义标签更加简单,无需重写繁琐的doStrartTag和doEndTag等方法,即使是带标签体的标签,也与不带标签体的标签处理方法完全相同,无须重写doAfterBody等方法,通常只需重写doTag方法。
案例说明:功能迭代其标签和简单标签
1.书写标签处理类
package mytag;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* 简单标签类
*/
public class HelloWorldSimpleTag extends SimpleTagSupport{
@Override
public void doTag() throws JspException, IOException {
getJspContext().getOut().write("HelloWorldSimpleTag");
}
}
package mytag;
import java.io.IOException;
import java.util.Collection;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
/**
* 简单标签处理类,继承SimpleTagSupport
*/
public class MyIteatorTag extends SimpleTagSupport {
//标签属性
private String bean;
public String getBean() {
return bean;
}
public void setBean(String bean) {
this.bean = bean;
}
//标签的处理方法,简单标签处理类,只需要重写doTag方法
@Override
public void doTag() throws JspException, IOException {
//从page scope中获取名为bean的集合属性
Collection<String> itemList = (Collection<String>)getJspContext().getAttribute(bean);
//遍历集合
for(String s:itemList){
//将集合的元素设置到page范围
getJspContext().setAttribute("item", s);
getJspBody().invoke(null);
}
}
}
2.创建TLD
<?xml version="1.0" encoding="GBK"?>
<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">
<!-- 标签版本 -->
<tlib-version>1.0</tlib-version>
<!-- 标签名称 -->
<short-name>MyIteatorTag</short-name>
<!-- 定义第一个个标签 -->
<tag>
<!-- 定义标签名称 -->
<name>helloWorld</name>
<!-- 定义标签的处理类 -->
<tag-class>mytag.HelloWorldSimpleTag</tag-class>
<!-- 定义标签体类空 -->
<body-content>empty</body-content>
</tag>
<!-- 定义第二个标签 -->
<tag>
<!-- 定义标签名称 -->
<name>iterator</name>
<!-- 定义标签处理类 -->
<tag-class>mytag.MyIteatorTag</tag-class>
<!-- 定义标签体,该标签体是不允许脚本的标签 -->
<body-content>scriptless</body-content>
<!-- 定义标签属性 -->
<attribute>
<name>bean</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3.配置WEB.XML (可省略)
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<jsp-config>
<taglib>
<taglib-uri>/iterator</taglib-uri>
<taglib-location>/WEB-INF/MyiteratorTag.tld</taglib-location>
</taglib>
</jsp-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
4.JSP调用
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@ taglib uri="/iterator" prefix="iterss" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>表达式语言-逻辑运算符</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<iterss:helloWorld/>
<%
//创建输出对象
List<String> a = new ArrayList<String>();
a.add("a");
a.add("b");
a.add("c");
//将a放入 page 范围内
pageContext.setAttribute("a", a);
%>
<iterss:iterator bean="a">
${item }
</iterss:iterator>
</body>
</html>