posts - 431,  comments - 344,  trackbacks - 0
          公告
           Don't Repeat Yourself
          座右銘:you can lose your money, you can spent all of it, and if you work hard you get it all back. But if you waste your time, you're never gonna get it back.
          公告本博客在此聲明部分文章為轉摘,只做資料收集使用。


          微信: szhourui
          QQ:109450684
          Email
          lsi.zhourui@gmail.com
          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          留言簿(15)

          隨筆分類(1019)

          文章分類(3)

          文章檔案(21)

          收藏夾

          Link

          好友博客

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 865453
          • 排名 - 44

          最新評論

          閱讀排行榜

          package com.rain.tag;

          import Java.io.IOException;
          import Java.util.Collection;
          import Java.util.Iterator;

          import javax.servlet.jsp.JspException;
          import javax.servlet.jsp.JspTagException;
          import javax.servlet.jsp.PageContext;
          import javax.servlet.jsp.tagext.BodyTagSupport;

          public class IterateTag extends BodyTagSupport {

           private String name;  //代表了在pageContext中表示的一個屬性的名字
           private Iterator it;     //代表要迭代的內容
           private String type;  //表示it中對象的類型
           
           public void setCollection(Collection collection){
            if(collection.size()>0){
             it=collection.iterator();
            }
           }
           
           public void setName(String name){
            this.name=name;
           }
           
           public void setType(String type){
            this.type=type;
           }

           @Override
           public int doAfterBody() throws JspException {
            // TODO Auto-generated method stub
            return continueNext(it);
           }

           @Override
           public int doEndTag() throws JspException {
            // TODO Auto-generated method stub
            try{
             if(bodyContent!=null){
              bodyContent.writeOut(bodyContent.getEnclosingWriter());
             }
            }catch(IOException e){
             throw new JspTagException("IO Error:"+e.getMessage());
            }
            return EVAL_PAGE;
           }

           @Override
           public int doStartTag() throws JspException {
            // TODO Auto-generated method stub
            if(it==null){
             return SKIP_BODY;
            }else{
             return continueNext(it);
            }
           }
           
           protected int continueNext(Iterator it)throws JspTagException{
            if(it.hasNext()){
             pageContext.setAttribute(name, it.next(), PageContext.PAGE_SCOPE);
             return EVAL_BODY_AGAIN;
            }else{
             return SKIP_BODY;
            }
           }
          }
          由于BodyTagSupport類實現了TagSupport,而TagSupport又實現了TterationTag接口。故在開發迭代標簽時可以直接從BodyTagSupport繼承。


          package com.rain.tag;

          import javax.servlet.jsp.tagext.TagData;
          import javax.servlet.jsp.tagext.TagExtraInfo;
          import javax.servlet.jsp.tagext.VariableInfo;

          //TagExtraInfo用于提供一些在標簽翻譯時相關的信息
          public class IterateTEI extends TagExtraInfo {

           public IterateTEI(){
            super();
           }

           @Override
           public VariableInfo[] getVariableInfo(TagData arg0) {
            // TODO Auto-generated method stub
            return new VariableInfo[]{
              new VariableInfo(arg0.getAttributeString("name"),
                               arg0.getAttributeString("type"),
                               true,
                               VariableInfo.NESTED)
            };
           }
          }
          需要說明的是VariableInfo的幾個常數,如下所示:
          1.NESTED:標簽中的參數在starttag到endtag之間是有效的。
          2.AT_BENGIN:標簽中的參數在標簽的開始到JSP頁面結束是有效的。
          3.AT_END:標簽中的參數在標簽的結束到JSP頁面結束是有效的。


          在標簽的描述中,必須同時指定標簽的實現類和IterateTEI類。
                 <tag>
                    <name>iterate</name>
                      <tag-class>com.rain.tag.IterateTag</tag-class>
                      <tei-class>com.rain.tag.IterateTEI</tei-class>
                    <body-content>jsp</body-content>
                    <attribute>
                      <name>collection</name>
                      <required>true</required>
                      <rtexprvalue>true</rtexprvalue>
                    </attribute>
                    <attribute>
                      <name>name</name>
                      <required>true</required>
                    </attribute>
                    <attribute>
                      <name>type</name>
                      <required>true</required>
                    </attribute>
                </tag>


          <?xml version="1.0" encoding="UTF-8"?>
          <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "<web-app>
           <taglib>
             <taglib-uri>/demotag</taglib-uri>
             <taglib-location>/WEB-INF/mytag.tld</taglib-location>
           </taglib>
          </web-app>

          <%@ page language="
          Java" contentType="text/html; charset=UTF-8"%>
          <jsp:directive.page import="Java.util.Collection"/>
          <%@ taglib uri="/demotag" prefix="mt" %>
          <html>
          <head>
          <title>Insert title here</title>
          </head>
          <body>
          <%
           String one="one";
           String two="two";
           String three="three";
           Collection list=new Java.util.ArrayList();
           list.add(one);
           list.add(two);
           list.add(three);
          %>
          <mt:iterate name="scott" type="String" collection="<%=list%>">
            <%=scott%><br>
          </mt:iterate>
          </body>
          </html>

          運行結果:
          one
          two
          three

          posted on 2007-01-22 16:15 周銳 閱讀(338) 評論(0)  編輯  收藏 所屬分類: Jsp
          主站蜘蛛池模板: 宜春市| 大田县| 海盐县| 营口市| 邳州市| 绥滨县| 柳河县| 永善县| 尼玛县| 平安县| 扎兰屯市| 东兴市| 南宁市| 乃东县| 凯里市| 安宁市| 忻城县| 古蔺县| 顺义区| 霍城县| 吴忠市| 安阳县| 海伦市| 建平县| 阿克陶县| 潮州市| 华池县| 临湘市| 饶阳县| 漯河市| 溧阳市| 北辰区| 资溪县| 务川| 绥中县| 井冈山市| 万盛区| 大荔县| 海淀区| 鸡西市| 静安区|