现在的位置: 首页 > 综合 > 正文

开发一个foreach标签

2017年10月03日 ⁄ 综合 ⁄ 共 2588字 ⁄ 字号 评论关闭

此标签和sun公司的一样可以迭代Map,List,数组

jsp文件为

<%@page import="java.util.*"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="/simple" prefix="c"%> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>foreach tag</title>
</head>
<body>
	<%
		List list = new ArrayList();
		list.add("aaa");
		list.add("bbb");
		list.add("ccc");
		request.setAttribute("list", list);
	%>
	<c:foreach var="str" items="${list}">
		${str }
	</c:foreach>
	<br>------------------------------<br>
	<%
		Map map = new HashMap();
		map.put("aa", "111");
		map.put("bb", "222");
		map.put("aa", "333");
		request.setAttribute("map", map);
	%>
	<c:foreach var="entry" items="${map }">
		${entry.key } = ${entry.value }
	</c:foreach>
	
	<br>------------------------------<br>
	
	<%
		Integer num[] = {1,2,3,4}; 
		request.setAttribute("num", num);
	%>
	<c:foreach var="i" items="${num }">
		${i }
	</c:foreach>
	<br>------------------------------<br>
	
	<%
		int arr[] = {1,2,3,4}; 
		request.setAttribute("arr", arr);
	%>
	<c:foreach var="i" items="${arr }">
		${i }
	</c:foreach>
</body>
</html>

标签处理器为

package cn.itcast.web.tag;

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.List;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

//开发foreach标签
public class ForeachTag extends SimpleTagSupport {

	private Object items;
	private String var;
	
	//单列或双列集合存储至此
	private Collection collection;
	
	public void setItems(Object items) {
		this.items = items;
		
		//将map转为单列集合
		if(items instanceof Map){
			Map map = (Map) items;
			collection = map.entrySet();
		}
		
		if(items instanceof Collection){
			collection = (List) items;
		}
		
		//统一对数组进行处理
		if(items.getClass().isArray()){
			this.collection = new ArrayList();
			int length = Array.getLength(items);
			for(int i=0; i<length; i++){
				Object value = Array.get(items, i);
				collection.add(value);
			}
		}
		
		
		/*if(items instanceof Object[]){
			Object[] obj =  (Object[]) items;
			collection = Arrays.asList(obj);  //此方法接受一个对象
		}*/
		
	}
	
	public void setVar(String var) {
		this.var = var;
	}
	
	@Override
	public void doTag() throws JspException, IOException {
		
		Iterator it = collection.iterator();
		while(it.hasNext()){
			Object obj = it.next();
			//对象存在域中
			this.getJspContext().setAttribute(var, obj);
			//标签执行
			this.getJspBody().invoke(null);
		}
	}
	
	
}

要记得在tld中对标签描述

  <tag>
        <name>foreach</name>
		<tag-class>cn.itcast.web.tag.ForeachTag</tag-class>
		<body-content>scriptless</body-content>
		
		<attribute>
			<name>var</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		
		<attribute>
			<name>items</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
    </tag>

抱歉!评论已关闭.