心得14--jsp遍历所有数据标签与转义标签

1.  开发遍历所有类型数据的标签

标签处理类:

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);

        for(int i=0;i<len;i++){

           collection .add( Array.get(items, i));

        }

           

      }

      Iterator it = collection.iterator();

      while(it.hasNext()){

        Object obj &
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值