jsp標(biāo)簽

              剛剛結(jié)束了一個(gè)項(xiàng)目,有點(diǎn)時(shí)間,所以先補(bǔ)充補(bǔ)充自己的知識(shí),昨天學(xué)了一下Jsp標(biāo)簽,感覺(jué)挺好的,可以使jsp頁(yè)面干凈不少,維護(hù)起來(lái)更方便了。
              下邊就開(kāi)始進(jìn)入正題:
          首先創(chuàng)建一個(gè)Pagination.tld文件,標(biāo)簽文件:
          <?xml   version="1.0"   encoding="UTF-8"?>
            <taglib>
                <tlibversion>1.0</tlibversion>
                <jspversion>1.1</jspversion>  
                <shortname>Pagination</shortname>  
                <tag>
                <name>Pagination</name>
                <tagclass>tags.Pagination</tagclass> 
                <attribute>
                    <name>allpage</name>
                    <required>true</required>
                    <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
                    <name>currentpage</name>
                    <required>true</required>
                    <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
                    <name>action</name>
                    <required>true</required>
                    <rtexprvalue>true</rtexprvalue>
                </attribute>
                <attribute>
                    <name>allcount</name>
                    <required>true</required>
                    <rtexprvalue>true</rtexprvalue>
                </attribute>
                </tag>
          </taglib>

          tlibversion: 標(biāo)簽庫(kù)版本
          jspversion: 標(biāo)簽庫(kù)依賴(lài)的jsp版本
          shortname:  標(biāo)簽庫(kù)名
          tag          開(kāi)始一個(gè)標(biāo)簽
          name         標(biāo)簽名
          tagclass     指定標(biāo)簽處理程序類(lèi)
          attribute    開(kāi)始一個(gè)標(biāo)簽屬性,可用于傳入?yún)?shù)
          name         屬性名
          required和rtexprvalue都是屬性的設(shè)置,均有2個(gè)屬性ture和false用來(lái)決定是否必須指定

          接下來(lái)創(chuàng)建該標(biāo)簽的處理程序類(lèi)Pagination.java:

          package tags;   
          import java.util.*;
          import javax.servlet.jsp.JspException;     
          import javax.servlet.jsp.tagext.TagSupport;         
          public class Pagination extends TagSupport {
              private String allpage; //總頁(yè)數(shù)
              private String currentpage; //當(dāng)前頁(yè)數(shù)
              private String action; //form請(qǐng)求地址
              private String allcount; //總記錄數(shù)
              public int doStartTag() throws JspException {     
                  try {
                      StringBuffer cont = new StringBuffer();
                      String context = "<form action='"+action+"'>" +
                          "<table><tr><td colspan='7' align='right'>" +
                          "<a>總頁(yè)數(shù):"+allpage+"</a>&nbsp" +
                          "<a>總記錄數(shù):"+allcount+"</a>&nbsp";
                          if(Integer.valueOf(currentpage)>2){
                              context = context + "<a href='"+action+"?num=1'>第一

          頁(yè)</a>&nbsp";
                          }
                         
                          if(Integer.valueOf(currentpage)>1){
                              context = context + "<a href='"+action+"?num="+

          (Integer.valueOf(currentpage)-1)+"'>前一頁(yè)</a>&nbsp";
                          }
                         
                              context = context + "<a >當(dāng)前

          頁(yè):"+currentpage+"</a>&nbsp";
                             
                          if(Integer.valueOf(currentpage)<Integer.valueOf(allpage)){
                              context = context + "<a href='"+action+"?num="+

          (Integer.valueOf(currentpage)+1)+"'>下一頁(yè)</a>&nbsp";
                          }
                         
                          if(Integer.valueOf(currentpage)<(Integer.valueOf(allpage)-

          1)){
                              context = context + "<a href='"+action+"?

          num="+allpage+"'>最后一頁(yè)</a>&nbsp";
                          }
                         
                          context = context + "</td><td align='center' width='15%'>" +
                                              "<input type='text'

          size='1' name='num' id='pagenum' value=''  />" +
                                              "<input

          type='submit' value='GO' id='gobutton'></td></tr></table>";
                         
                      pageContext.getOut().print(context);
                  }catch(Exception e) {
                      throw new JspException("SimpleTag:"+e.getMessage());     
                  }
                  return EVAL_BODY_INCLUDE;         
              }     
              public int doEndTag() {     
                  return EVAL_PAGE;     
              }
              public void setAllpage(String allpage) {
                  this.allpage = allpage;
              }
              public void setCurrentpage(String currentpage) {
                  this.currentpage = currentpage;
              }
              public void setAction(String action) {
                  this.action = action;
              }
              public void setAllcount(String allcount) {
                  this.allcount = allcount;
              }
          }

          最后是要顯示的Jsp頁(yè)index.jsp:
            <%@ page language="java" pageEncoding="utf-8"%>
            <%@ taglib uri="/WEB-INF/tags/Pagination.tld" prefix="Pagination" %>
            <html>
            <head>
                <script type="text/javascript" src="js/Time.js"></script>
            </head>
            <body>
                <Pagination:Pagination allcount="20" action="servlet" currentpage="1" allpage="200"></Pagination:Pagination>
            </body>
            </html>

          posted on 2007-10-30 14:34 dripstone 閱讀(322) 評(píng)論(0)  編輯  收藏


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          <2007年10月>
          30123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)(1)

          隨筆檔案(16)

          友情鏈接

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 缙云县| 合肥市| 垫江县| 屯昌县| 临沂市| 孝昌县| 麻栗坡县| 诸暨市| 鄂伦春自治旗| 克什克腾旗| 青冈县| 闸北区| 高雄市| 通海县| 石首市| 平陆县| 鲜城| 祥云县| 文登市| 定日县| 北票市| 徐闻县| 元江| 南靖县| 冷水江市| 区。| 巨鹿县| 调兵山市| 扶沟县| 江安县| 永德县| 通海县| 顺义区| 房山区| 手游| 汉阴县| 儋州市| 若尔盖县| 三台县| 新乡市| 泰来县|