开发标签开发标签开发迭代标签源代码

 

•    开发<c:if>标签

package com.hbsi.web.tag;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.JspFragment;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class IfTag extends SimpleTagSupport {

         private boolean test;

 

         public void setTest(boolean test) {

                   this.test = test;

         }

 

         @Override

         public void doTag() throws JspException, IOException {

                   // TODO Auto-generated method stub

                   if(test){

                            //处理标签体

                            JspFragment jf=this.getJspBody();

                            jf.invoke(null);

                   }

         }

        

}

•    开发<c:if><c:else>标签

package com.hbsi.web.tag;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.JspFragment;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class ChooseTag extends SimpleTagSupport {

         private boolean flag=false;

 

         public boolean isFlag() {

                   return flag;

         }

 

         public void setFlag(boolean flag) {

                   this.flag = flag;

         }

 

         @Override

         public void doTag() throws JspException, IOException {

                   // TODO Auto-generated method stub

                   //对标签体处理

                   JspFragment jf=this.getJspBody();

                   jf.invoke(null);

         }

 

}

 

 

package com.hbsi.web.tag;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class OtherwiseTag extends SimpleTagSupport {

 

         @Override

         public void doTag() throws JspException, IOException {

                   // TODO Auto-generated method stub

         ChooseTag parent=(ChooseTag) this.getParent();

         if(!parent.isFlag()){

                  

                   this.getJspBody().invoke(null);

                   parent.setFlag(true);

                  

         }

         }

        

 

}

 

 

package com.hbsi.web.tag;

 

import java.io.IOException;

 

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

 

public class WhenTag extends SimpleTagSupport {

         private boolean test;

 

         public void setTest(boolean test) {

                   this.test = test;

         }

 

         @Override

         public void doTag() throws JspException, IOException {

                   // TODO Auto-generated method stub

                   //获取父标签对象

                   ChooseTag parent=(ChooseTag)this.getParent();

                   if(test&&!parent.isFlag()){

                            //处理标签体

                            this.getJspBody().invoke(null);

                            //有分支执行过设为真

                            parent.setFlag(true);

                           

                   }

                  

         }

 

}

•    开发迭代标签

•    package com.hbsi.web.tag;

•     

•    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.tagext.SimpleTagSupport;

•     

•    public class ForeachTag extends SimpleTagSupport {

•         private Object items;

•         private String var;

•         public void setItems(Object items) {

•                   this.items = items;

•         }

•         public void setVar(String var) {

•                   this.var = var;

•         }

•         @Override

•         public void doTag() throws JspException, IOException {

•                   // TODO Auto-generated method stub

•                   //得到集合对象

•                   //List list=(List) items;

•                   //Iterator it=list.iterator();

•                   Collection collection=null;

•                   if(items instanceof Map){

•                            Map map=(Map)items;

•                            //两列的集合转换成单列

•                            collection=map.entrySet();

•                   }else if(items instanceof Collection){

•                            collection=(Collection) items;

•                   }

•    //                     else if(items instanceof Object[]){

•    //          

•    //                     Object[] objs=(Object[]) items;

•    //                     //倒换到一个一个集合中

•    //                     collection=Arrays.asList(objs);

•    //                    

•    //           }

•                   //应用反射机制

•                   else if(items.getClass().isArray()){

•                            collection=new ArrayList();

•                            int length=Array.getLength(items);

•                            for(int i=0;i<length;i++){

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

•                            }

•                           

•                   }

•                   Iterator it=collection.iterator();

•                   while(it.hasNext()){

•                            Object obj=it.next();//迭代出来的一个元素

•                            this.getJspContext().setAttribute(var, obj);

•                   this.getJspBody().invoke(null);

•                  

•                  

•                  

•                  

•                  

•                  

•                  

•                  

•                   }

•         }

•    }

 

 

你可能感兴趣的:(c,object,null,iterator,Class,import)