<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@taglib uri ="/jsp2-tagexample" prefix="cc"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP '1.jsp' starting page</title> </head>
<body> <% List list=new ArrayList(); list.add("aaa"); list.add("bbb"); list.add("ccc"); list.add("ddd"); request.setAttribute("list",list); %> <cc:foreach items="${list}" var="str"> ${str } </cc:foreach>
</body> </html> |
<tag> <name>foreach</name> <tag-class>cn.itcast.web.tag.eaxmple.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>true</rtexprvalue> </attribute> </tag> |
package cn.itcast.web.tag.eaxmple;
import java.io.IOException; import java.util.Iterator; import java.util.List;
import javax.servlet.jsp.JspException; import javax.servlet.jsp.PageContext; import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForeachTag extends SimpleTagSupport {
private String var; private Object items;
public void setVar(String var) { this.var = var; }
public void setItems(Object items) { this.items = items; }
@Override public void doTag() throws JspException, IOException { List list= (List) items; Iterator it=list.iterator(); while(it.hasNext()){ String value=(String) it.next(); this.getJspContext().setAttribute(var, value); this.getJspBody().invoke(null); } } } |
最重要的是利用反射和Array统一处理数组
<%@ page language="java" pageEncoding="UTF-8" isELIgnored="false"%> <%@page import="java.util.ArrayList"%> <%@page import="java.util.List"%> <%@page import="java.util.HashMap"%> <%@page import="java.util.Map"%> <%@taglib uri ="/jsp2-tagexample" prefix="cc"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>My JSP '1.jsp' starting page</title> </head>
<body> ______list_____<br> <% List list=new ArrayList(); list.add("nnn"); list.add("bbb"); list.add("ccc"); list.add("ddd"); request.setAttribute("list",list); %> <cc:foreach2 items="${list}" var="str"> ${str }<br/> </cc:foreach2>
______map_____<br> <% Map map=new HashMap(); map.put("aa","111"); map.put("bb","222"); map.put("cc","333"); request.setAttribute("map",map); %> <cc:foreach2 items="${map}" var="entry"> ${entry.key } :${entry.value }<br/> </cc:foreach2>
______Integer_____<br> <% Integer num[]={1,2,3,4}; request.setAttribute("num",num); %> <cc:foreach2 items="${num}" var="i"> ${i }<br/> </cc:foreach2>
______String_____<br> <% String strs[]={"sss","mmm"}; request.setAttribute("strs",strs); %> <cc:foreach2 items="${strs}" var="str"> ${str }<br/> </cc:foreach2> ______int_____<br> <% int num2[]={11,22,33,44}; request.setAttribute("num2",num2); %> <cc:foreach2 items="${num2}" var="i"> ${i } </cc:foreach2> </body> </html> |
package cn.itcast.web.tag.eaxmple;
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.PageContext; import javax.servlet.jsp.tagext.SimpleTagSupport;
public class ForeachTag2 extends SimpleTagSupport {
private String var; private Object items; private Collection collection; public void setVar(String var) { this.var = var; }
public void setItems(Object items) { this.items = items; if(items==null) collection=null; if(items instanceof Collection) { collection=(Collection) items; } if(items instanceof Map){ Map map=(Map)items; collection=map.entrySet(); } //统一处理数组,不管是对象数组还是基本数据类型的数组 if(items.getClass().isArray()){ this.collection=new ArrayList(); int len=Array.getLength(items); for(int i=0;i<len;i++) this.collection.add(Array.get(items, i)); }
/* if(items instanceof Object[]){ Object obj[]=(Object[]) items; collection=Arrays.asList(obj); } //sun公司在这里就是把八种基本数据类型全部一一处理,效率上应该高点 if(items instanceof int[]){ int arr[]=(int[])items; this.collection=new ArrayList(); for(int i:arr){ this.collection.add(i); } }*/ }
@Override public void doTag() throws JspException, IOException { if( collection==null) return; Iterator it=collection.iterator(); while(it.hasNext()){ Object value=it.next(); this.getJspContext().setAttribute(var, value); this.getJspBody().invoke(null); } } } |