[代码] 运用到了数组反射的一些常识 //1:EachTag package cn.itcast.apsliyuan.tag; import java.lang.reflect.Array; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; import java.util.Li http://www.starkp.com/linked/20130217.do; st; import java.util.Map; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; public class EachTag extends TagSupport { /** * 反序列化 */ private static final long serialVersionUID = 7239081890975877992L; private String var; private Iterator it; public void setVar(String var) { this.var = var; } public void setItems(Object object) { if (object instanceof Collection) { it = ((Collection) object).iterator(); } else if (object instanceof Map) { it = ((Map) object).entrySet().iterator(); } else if (object.getClass().isArray()) {//数组反射,尽管影响了功率,可是比每种类型比较是简略多了 List list = new ArrayList(); int len = Array.getLength(object); for (int i = 0; i < len; i ) { list.add(Array.get(object, i)); } it = list.iterator(); } } // 只会履行一次 @Override public int doStartTag() throws JspException { if (it.hasNext()) { pageContext.setAttribute(var, it.next()); return EVAL_BODY_INCLUDE; } else { return SKIP_BODY; } } @Override public int doAfterBody() throws JspException { // TODO Auto-generated method stub if (it.hasNext()) { pageContext.setAttribute(var, it.next()); return EVAL_BODY_AGAIN;// 再次履行body有些 } else { return SKIP_BODY; } } @Override public int doEndTag() throws JspException { pageContext.removeAttribute(var); return EVAL_PAGE; } } http://www.starkp.com/linked/20130217.do;
转载于:https://www.cnblogs.com/fpqqchao/archive/2013/02/18/2915106.html