<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="/example" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>开发迭代标签(foreach)(重复执行标签体案例)</title>
</head>
<body>
<%--模拟servlet的代码 --%>
<%
List<String> list = new ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
request.setAttribute("list", list);
%>
<c:foreach2 var="str" items="${list }">
${str}<br/>
</c:foreach2>
<br/><hr/>
<%--模拟servlet的代码 --%>
<%
Map<String, String> map = new LinkedHashMap<String, String>();
map.put("aa", "111");
map.put("bb", "222");
map.put("cc", "333");
request.setAttribute("map", map);
%>
<c:foreach2 var="entry" items="${map }">
${entry.key} = ${entry.value }<br/>
</c:foreach2>
<br/><hr/>
<%--模拟servlet的代码 --%>
<%
Integer[] num = {1,2,3};
request.setAttribute("num", num);
%>
<c:foreach2 var="i" items="${num }">
${i}<br/>
</c:foreach2>
<br/><hr/>
<%--模拟servlet的代码 --%>
<%
int[] arr = {1,2,3};
request.setAttribute("arr", arr);
%>
<c:foreach2 var="i" items="${arr }">
${i}<br/>
</c:foreach2>
<br/><hr/>
<%--模拟servlet的代码 --%>
<%
boolean[] b = {true,false,true};
request.setAttribute("b", b);
%>
<c:foreach2 var="i" items="${b }">
${i}<br/>
</c:foreach2>
</body>
</html>
package cn.itcast.web.tag.example;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//开发迭代标签(foreach)
//不管是双列集合还是数组还是单列集合,全部都转成单列集合进行迭代
public class ForeachTag2 extends SimpleTagSupport {
// 因为传进来的可能是List也可能是Map也可能是数组,所以用Object类型
private Object items;
//var的类型要是String因为他是迭代的关键字,存入pageContext中的key
private String var;
private Collection collection;
public void setItems(Object items) {
this.items = items;
//在传入集合的时候首先判断是不是Collection、Map、数组,全都转成单列集合存入collection变量中
if(items instanceof Collection) { //list set
collection = (Collection) items;
}
if(items instanceof Map) {
Map map = (Map) items;
collection = map.entrySet();
}
//后面的全都是数组判断,太多了,用反射简化
//通过Array就可以对任意数组进行操作
if(items.getClass().isArray()) {//判断是不是数组
this.collection = new ArrayList();
//得到数组长度用反射包中的Array类
int length = Array.getLength(items);
//迭代数组
for(int i=0;i<length;i++) {
Object value = Array.get(items, i);
this.collection.add(value);
}
}
/*
if(items instanceof Object[]) {
Object[] obj = (Object[]) items;
collection = Arrays.asList(obj);
}
//还要判断是不是基本类型的数组, 8个基本类型依次判断,sun就是这么做的
if(items instanceof int[]) {
int[] arrInt = (int[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(int i : arrInt) {
collection.add(i);
}
}
if(items instanceof long[]) {
long[] arrLong = (long[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(long i : arrLong) {
collection.add(i);
}
}
if(items instanceof short[]) {
short[] arrShort = (short[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(short i : arrShort) {
collection.add(i);
}
}
if(items instanceof byte[]) {
byte[] arrByte = (byte[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(byte i : arrByte) {
collection.add(i);
}
}
if(items instanceof boolean[]) {
boolean[] arrBoolean = (boolean[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(boolean i : arrBoolean) {
collection.add(i);
}
}
if(items instanceof double[]) {
double[] arrDouble = (double[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(double i : arrDouble) {
collection.add(i);
}
}
if(items instanceof char[]) {
char[] arrChar = (char[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(char i : arrChar) {
collection.add(i);
}
}
if(items instanceof float[]) {
float[] arrFloat = (float[]) items;
//不可以用Arrays.asList();这个方法,不能把基本类型转换成集合
collection = new ArrayList();
for(float i : arrFloat) {
collection.add(i);
}
}
*/
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
//对collection进行迭代
Iterator it = collection.iterator();
while(it.hasNext()) {
Object value = it.next();
this.getJspContext().setAttribute(var, value);
this.getJspBody().invoke(null);
}
}
}
<?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>example</short-name><!-- 描述文件的名称 -->
<uri>/example</uri><!-- 描述文件的uri -->
<tag>
<name>referer</name>
<tag-class>cn.itcast.web.tag.example.RefererTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>site</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>page</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>if</name>
<tag-class>cn.itcast.web.tag.example.IfTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>choose</name>
<tag-class>cn.itcast.web.tag.example.ChooseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>when</name>
<tag-class>cn.itcast.web.tag.example.WhenTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>otherwise</name>
<tag-class>cn.itcast.web.tag.example.OtherWiseTag</tag-class>
<body-content>scriptless</body-content>
</tag>
<tag>
<name>foreach</name>
<tag-class>cn.itcast.web.tag.example.ForeachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>foreach2</name>
<tag-class>cn.itcast.web.tag.example.ForeachTag2</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>