jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/iteratorall" prefix="my6" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'itrstorcollection.jsp' starting page</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>
<%
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("123");
list.add("wasd");
pageContext.setAttribute("listObj",list);
%>
遍历List<br/>
<my6:forEach items="${listObj}" var="mm">
${mm} <br/>
</my6:forEach>
<hr/>
<%
Map<String,String> map = new HashMap<String,String>();
map.put("11111","11111");
map.put("22222","222222");
map.put("33333","33333");
pageContext.setAttribute("mapObj",map);
%>
遍历Map<br/>
<my6:forEach items="${mapObj}" var="map">
${map.key} ---- ${map.value} <br/>
</my6:forEach>
<hr/>
遍历String[]<br/>
<%
String[] strs = {"aaaa","bbbbbb","cccccc"};
pageContext.setAttribute("strsObj",strs);
%>
<my6:forEach items="${strsObj}" var="sss">
${sss} --<br/>
</my6:forEach>
</body>
</html>
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">
<tlib-version>1.0</tlib-version>
<short-name>my6</short-name>
<uri>http://www.cgmcc.com/jsp/iteratorall</uri>
<tag>
<name>forEach</name>
<tag-class>com.cgm.itratorcol.IteratorCollection</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>true</rtexprvalue>
</attribute>
</tag>
</taglib>
java
package com.cgm.itratorcol;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class IteratorCollection extends SimpleTagSupport{
/*
<my:myList items="${listObj}" var="yyyyy">
${yyyyy} ## <br/>
</my:myList>
*/
private Iterator<Object> items;
private String var;
public void setItems(Object items) {
//list
if(items instanceof List){
this.items = ((List)items).iterator();
}
//map
if(items instanceof Map){
this.items = ((Map)items).entrySet().iterator();
}
//object[]
if(items instanceof Object[]){
this.items = Arrays.asList((Object[])items).iterator();
}
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
//完成功能:将获得的list信息遍历输出到浏览器
while(items.hasNext()){
Object str = items.next();
//将结果放置到作用域
this.getJspContext().setAttribute(var, str);
//将el表达式的结果数据到浏览器
this.getJspBody().invoke(null);
}
}
}
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.cgmcc.com/jsp/iteratorall" prefix="my6" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'itrstorcollection.jsp' starting page</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>
<%
List<String> list = new ArrayList<String>();
list.add("abc");
list.add("123");
list.add("wasd");
pageContext.setAttribute("listObj",list);
%>
遍历List<br/>
<my6:forEach items="${listObj}" var="mm">
${mm} <br/>
</my6:forEach>
<hr/>
<%
Map<String,String> map = new HashMap<String,String>();
map.put("11111","11111");
map.put("22222","222222");
map.put("33333","33333");
pageContext.setAttribute("mapObj",map);
%>
遍历Map<br/>
<my6:forEach items="${mapObj}" var="map">
${map.key} ---- ${map.value} <br/>
</my6:forEach>
<hr/>
遍历String[]<br/>
<%
String[] strs = {"aaaa","bbbbbb","cccccc"};
pageContext.setAttribute("strsObj",strs);
%>
<my6:forEach items="${strsObj}" var="sss">
${sss} --<br/>
</my6:forEach>
</body>
</html>
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">
<tlib-version>1.0</tlib-version>
<short-name>my6</short-name>
<uri>http://www.cgmcc.com/jsp/iteratorall</uri>
<tag>
<name>forEach</name>
<tag-class>com.cgm.itratorcol.IteratorCollection</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>true</rtexprvalue>
</attribute>
</tag>
</taglib>
java
package com.cgm.itratorcol;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class IteratorCollection extends SimpleTagSupport{
/*
<my:myList items="${listObj}" var="yyyyy">
${yyyyy} ## <br/>
</my:myList>
*/
private Iterator<Object> items;
private String var;
public void setItems(Object items) {
//list
if(items instanceof List){
this.items = ((List)items).iterator();
}
//map
if(items instanceof Map){
this.items = ((Map)items).entrySet().iterator();
}
//object[]
if(items instanceof Object[]){
this.items = Arrays.asList((Object[])items).iterator();
}
}
public void setVar(String var) {
this.var = var;
}
@Override
public void doTag() throws JspException, IOException {
//完成功能:将获得的list信息遍历输出到浏览器
while(items.hasNext()){
Object str = items.next();
//将结果放置到作用域
this.getJspContext().setAttribute(var, str);
//将el表达式的结果数据到浏览器
this.getJspBody().invoke(null);
}
}
}