jsp遍历所有数据标签与转义标签
1. 开发遍历所有类型数据的标签
[java]
<span style="color:#009900;BACKGROUND-COLOR: #ffffff">标签处理类:
</span>
<span style="BACKGROUND-COLOR: #ffffff">package com.csdn.web.example;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
publicclass ForEachAll extends SimpleTagSupport{
private Collection collection;
private String var;
private Object items;
publicvoid setVar(String var) {
this.var = var;
}
publicvoid setItems(Object items) {
this.items = items;
}
@Override
publicvoid doTag() throws JspException, IOException {
//判断是否是Map 下面的三个判断可以在doTage()方法中也可以在setItems()方法中
if(itemsinstanceof Map){
//这里要把jsp页面传进来的属性强转为Map类型,不能new HashMap
Map map = (Map) items;
collection = map.entrySet();
}
//判断是否是set、list
if(itemsinstanceof Collection){
collection = (Collection) items;
}
//判断是否是数组,各种数组
if(items.getClass().isArray()){
collection = new ArrayList();
int len = Array.getLength(items);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>