标签处理器类
package com.ljh.test;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Collection;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;
@SuppressWarnings("unchecked")
public class ForEachTag extends SimpleTagSupport{
private Object items;
private String var;
private Collection collection;
public void setCollection(Collection collection) {
this.collection = collection;
}
public void setItems(Object items) {
this.items = items;
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
JspFragment jf = this.getJspBody();
tagInit();
if(collection!=null){
for(Object obj:collection){
this.getJspContext().setAttribute(var, obj);
jf.invoke(null);
}
}
}
@SuppressWarnings("unchecked")
public void tagInit(){
if(items instanceof Collection){
collection = (Collection)items;
}
if(items instanceof Map){
Map map = (Map)items;
collection = map.entrySet();
}
if(items instanceof Object[]){
collection = Arrays.asList((Object[])items);
}else if(items.getClass().isArray()){
int length = Array.getLength(items);
Object o[] = new Object[length];
for(int i=0;i<length;i++){
Object obj = Array.get(items, i);
o[i] = obj;
}
collection = Arrays.asList((Object[])o);
}
}
}
标签tld文件
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>debug</short-name>
<uri>ljh</uri>
<tag>
<name>forEach</name>
<tag-class>com.ljh.test.ForEachTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
测试页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="ljh" uri="ljh"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>forEach Test</title>
</head>
<body>
<%
List list = new ArrayList();
list.add("1");
list.add("2");
list.add("3");
list.add("4");
request.setAttribute("list",list);
%>
<%
Map map = new HashMap();
map.put("key1","value1");
map.put("key2","value2");
map.put("key3","value3");
map.put("key4","value4");
request.setAttribute("map",map);
%>
<%
Integer arr[] = {1,2,3,4};
request.setAttribute("arr",arr);
%>
<%
int arrs[] = {5,6,7,8};
request.setAttribute("arrs",arrs);
%>
<%
boolean arr1[] = {true,false,false,true};
request.setAttribute("arr1",arr1);
%>
<ljh:forEach var="temp" items="${list}">
${temp}<br>
</ljh:forEach>
<hr>
<ljh:forEach var="temp" items="${map}">
${temp}<br>
</ljh:forEach>
<hr>
<ljh:forEach var="temp" items="${arr}">
${temp}<br>
</ljh:forEach>
<hr>
<ljh:forEach var="temp" items="${arrs}">
${temp}<br>
</ljh:forEach>
<ljh:forEach var="temp" items="${arr1}">
${temp}<br>
</ljh:forEach>
<hr>
</body>
</html>