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> " +
"<a>總記錄數(shù):"+allcount+"</a> ";
if(Integer.valueOf(currentpage)>2){
context = context + "<a href='"+action+"?num=1'>第一
頁(yè)</a> ";
}
if(Integer.valueOf(currentpage)>1){
context = context + "<a href='"+action+"?num="+
(Integer.valueOf(currentpage)-1)+"'>前一頁(yè)</a> ";
}
context = context + "<a >當(dāng)前
頁(yè):"+currentpage+"</a> ";
if(Integer.valueOf(currentpage)<Integer.valueOf(allpage)){
context = context + "<a href='"+action+"?num="+
(Integer.valueOf(currentpage)+1)+"'>下一頁(yè)</a> ";
}
if(Integer.valueOf(currentpage)<(Integer.valueOf(allpage)-
1)){
context = context + "<a href='"+action+"?
num="+allpage+"'>最后一頁(yè)</a> ";
}
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) 編輯 收藏