自定義標籤的案例

 

一、<c:if>標籤

       標籤處理器

    private boolean test;

 

    public void setTest(boolean test) {

       this.test = test;

    }

 

    @Override

    public void doTag() throws JspException, IOException {

       // TODO Auto-generated method stub

       JspFragment jf=this.getJspBody();

       jf.invoke(null);

    }

    Tld文件

    <tag>

  <name>if</name>

  <tag-class>com.hbsi.web.tag.IfTag</tag-class>

  <body-content>scriptless</body-content>

  <attribute>

   <name>test</name>

   <required>true</required>

   <rtexprvalue>true</rtexprvalue>

  </attribute>

 </tag>

.jsp文件

<%@taglib uri="http://www.ping.com" prefix="c" %>

    <%

    session.setAttribute("user","zhangsan");

   %>

    <c:if test="${user!=null}">

    i can!<br/>

    </c:if>

 

二、<c:foreach><c:when/><c:otherwise/></c:foreach>標籤

       父標籤<c:foreach></c:foreach>處理器

 

       private boolean flag=false;

   

    public boolean isFlag() {

       return flag;

    }

 

    public void setFlag(boolean flag) {

       this.flag = flag;

    }

 

    @Override

    public void doTag() throws JspException, IOException {

       JspFragment jf=this.getJspBody();

       jf.invoke(null);

      

    }

    子標籤<c:when/>處理器

    private boolean test;

 

    public void setTest(boolean test) {

       this.test = test;

    }

 

    @Override

    public void doTag() throws JspException, IOException {

      

       //獲取父標籤對象

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

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

              //處理該標籤體

              this.getJspBody().invoke(null);

              parent.setFlag(true);

           }

 

    }

子標籤<c:otherwise/>處理器

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

       }

    }

Tld文件

    <tag>

  <name>choose</name>

  <tag-class>com.hbsi.web.tag.ChooseTag</tag-class>

  <body-content>scriptless</body-content>

 </tag>

 <tag>

  <name>when</name>

  <tag-class>com.hbsi.web.tag.WhenTag</tag-class>

  <body-content>scriptless</body-content>

  <attribute>

   <name>test</name>

   <required>true</required>

   <rtexprvalue>true</rtexprvalue>

  </attribute>

 </tag>

  <tag>

  <name>otherwise</name>

  <tag-class>com.hbsi.web.tag.OtherwiseTag</tag-class>

  <body-content>scriptless</body-content>

 </tag>

.jsp文件

<c:choose>

    <c:when test="false">aaaaaaaaa</c:when>

    <c:otherwise>bbbbbb</c:otherwise>

  </c:choose>

 

三、<c:Foreach></c: Foreach >

       包括不同類型的迭代情況

       3-1、鏈表:List

       3-2、映射:Map

       3-3、對象數組:

       3-4、普通數組

<c:Foreach></c: Foreach >標籤處理器

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 instanceof int[]){

          

       }*/

       else if(items.getClass().isArray()){//獲的是class對象,每一種數據類型都對應着一種class

           collection=new ArrayList();

           int length=Array.getLength(items);

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

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

           }

       }

       //不管是那種類型,最終都要導到collection中

       Iterator it=collection.iterator();

       while(it.hasNext()){

           Object obj=it.next();//集合中的一個元素

           this.getJspContext().setAttribute(var,obj);//把集合中的元素存到某個作用域中

           this.getJspBody().invoke(null);//處理標籤體

            

       }

    }

Tld文件

<tag>

  <name>foreach</name>

  <tag-class>com.hbsi.web.tag.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>

.jsp文件

<%

    List list=new ArrayList();

    list.add("ddd");

    list.add("rrr");

    list.add("ggg");

    list.add("hh");

    list.add("dv");

    request.setAttribute("list",list);

     %>

    <c:foreach items="${list}" var="str">${str}</c:foreach><%--var屬性的值就是存儲在作用域中的屬性的名稱,讓${str} 可以找到吸納供應的元素--%>

    <br/>-------------------------<br/>

    <%

       Map map=new HashMap();

       map.put("aa","dudu");

       map.put("bb","hehe");

       map.put("cc","wuwu");

       map.put("dd","lele");

       request.setAttribute("map",map);

     %>

    <c:foreach items="${map }" var="name">

       ${name}

    </c:foreach>

    <br/>-------------------------<br/>

    <%

       Integer[] num={1,3,4,5,6};//對象類型的數組

       request.setAttribute("num",num);

     %>

    <c:foreach items="${num }" var="i">

       ${i}

    </c:foreach>

    <br/>-------------------------<br/>

    <%

       int[]arr={1,2,3,4};//普通數組

       request.setAttribute("arr",arr);

     %>

     <c:foreach items="${arr }" var="index">

       ${index}

    </c:foreach>

    <br/>-------------------------<br/>

    <%

       double[]dd={1.0,2.0,3.5,4.4};

       request.setAttribute("dd",dd);

     %>

     <c:foreach items="${dd }" var="index">

       ${index}

    </c:foreach>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章