效果如图:
1、JSP规范1.1版本后增加了自定义标签库。实现自定义标签的步骤
(1)开发自定义标签处理类。
(2)建立*.tld文件。
(3)在web.xml中增加自定义标签的定义。
(4)在jsp中使用自定义标签。
2、自定义标签类
(1)继承javax.servlet.jsp.tagext.TagSupport
(2)标签类属性,及每个属性的getter和setter方法
(3)重写doStartTag或doEndTag方法。当jsp解析这个标签的时候,在“<”处触发 doStartTag 事件,在“>”时触发 doEndTag 事件。通常在 doStartTag 里进行初始化,流程选择操作,在 doEndTag 里后续页面输出控制。
import java.io.IOException; import javax.servlet.jsp.JspException; import javax.servlet.jsp.tagext.TagSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * * @author liuxi */ public class PageThirdTag extends TagSupport { private static final Log log = LogFactory.getLog(PageTwoTag.class); private String formName; private String curPage; private String showPages; private String totalPages; private String PREVIOUS_PAGE = "上一页"; private String NEXT_PAGE = "下一页 "; public String getHref(int number) { return "Javascript:ToPage(" + number + ");"; } public String goHref(int number) { return " <a href=\"" + getHref(number) + "\" class=\"pagebox\">" + number + "</a>"; } public int doEndTag() throws JspException { int showPages = Integer.parseInt(this.showPages); int curpage = Integer.parseInt(this.curPage); int totalPages = Integer.parseInt(this.totalPages); StringBuffer strBuf = new StringBuffer(512); // 总页数 int pagecount = totalPages; // 初始化值 if (curpage == 0) { curpage = 1; } else { if (curpage <= 0) { curpage = 1; } if (curpage > pagecount) { curpage = pagecount; } } strBuf.append("<style type='text/css'>"); strBuf.append(".pagebox{margin-left:2px;padding:3px 5px 3px 5px; border:1px solid #fff; background-color:#ebebeb;color:#FFFFFF; font-size:12px;}"); strBuf.append(".cpagebox{margin-left:2px;padding:3px 5px 3px 5px; border:1px gray; background-color:#ebebeb; color:red; font-size:12px;}"); strBuf.append(".vpagebox{margin-left:2px;padding:3px 5px 3px 5px; background-color:#FFFFFF; color:#000000;font-size:12px;}"); strBuf.append("</style>"); strBuf.append("<script language='JavaScript' type='text/JavaScript'>"); strBuf.append("function ToPage(p) { \n"); strBuf.append(" window.document." + formName + ".pageNo.value=p;\n"); strBuf.append(" window.document." + formName + ".submit();\n"); strBuf.append("}</script>"); if (curpage > 1) { strBuf.append("<a href=\"" + getHref(curpage - 1) + "\" class=\"pagebox\" >" + PREVIOUS_PAGE + "</a>"); } // 分页 if (pagecount <= showPages + 2) { for (int i = 1; i <= pagecount; i++) { if (i == curpage) { strBuf.append("<font class=\"cpagebox\">" + i + "</font>"); } else { strBuf.append(goHref(i)); } } } else { if (curpage < showPages) { for (int i = 1; i <= showPages; i++) { if (i == curpage) { strBuf.append("<font class=\"cpagebox\">" + i + "</font>"); } else { strBuf.append(goHref(i)); } } strBuf.append("<font class=\"vpagebox\">...</font>"); strBuf.append(goHref(pagecount)); } else if (curpage > pagecount - showPages + 1) { // 右边 strBuf.append(goHref(1)); strBuf.append("<font class=\"vpagebox\">...</font>"); for (int i = pagecount - showPages + 1; i <= pagecount; i++) { if (i == curpage) { strBuf.append("<font class=\"cpagebox\">" + i + "</font>"); } else { strBuf.append(goHref(i)); } } } else { // 中间 strBuf.append(goHref(1)); //strBuf.append(goHref(2)); strBuf.append("<font class=\"vpagebox\">...</font>"); int offset = (showPages - 2) / 2; for (int i = curpage - offset; i <= curpage + offset; i++) { if (i == curpage) { strBuf.append("<font class=\"cpagebox\">" + i + "</font>"); } else { strBuf.append(goHref(i)); } } strBuf.append("<font class=\"vpagebox\">...</font>"); strBuf.append(goHref(pagecount)); } } // 显示下-页 if (curpage != pagecount) { // 加上链接 curpage+1 strBuf.append("<a href=\"" + getHref(curpage + 1) + "\" class=\"pagebox\" >" + NEXT_PAGE + "</a>"); } strBuf.append("<input name='pageNo' type='hidden' size='3' length='3' />"); try { pageContext.getOut().println(strBuf.toString()); } catch (IOException e) { e.printStackTrace(); log.debug(e.getMessage()); } return EVAL_PAGE; } public String getFormName() { return formName; } public void setFormName(String formName) { this.formName = formName; } public String getCurPage() { return curPage; } public void setCurPage(String curPage) { this.curPage = curPage; } public String getShowPages() { return showPages; } public void setShowPages(String showPages) { this.showPages = showPages; } public String getTotalPages() { return totalPages; } public void setTotalPages(String totalPages) { this.totalPages = totalPages; } }
说明:
(1)如何输出到jsp页面:调用pageContext.getOut().println()。
(2)输出后如何作处理,函数会返回几个值之一。EVAL_PAGE 表示tag已处理完毕,返回jsp页面。
3、建立self.tld 文件
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <description>TEST Tag library</description> <display-name>TEST Tag</display-name> <tlib-version>1.0</tlib-version> <short-name>test</short-name> <uri>/test</uri> <tag> <description>Page Info</description> <name>pagethird</name> <tag-class>com.test.web.tag.PageThirdTag</tag-class> <body-content>empty</body-content> <attribute> <description>the name of the current form</description> <name>formName</name> <required>true</required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <description>Show Records</description> <name>showPages</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description>Current Page</description> <name>curPage</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <description>Total Pages</description> <name>totalPages</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
说明:
short-name:taglib的名称。
name:tag的名字。
name:tag的名字。
body-content:指tag之间的内容。
required:是否必填属性。
rtexprvalue:是否支持动态传值。
4、web.xml中加入自定义标签定义
<jsp-config> <taglib> <taglib-uri>test</taglib-uri> <taglib-location>/WEB-INF/tld/self.tld</taglib-location> </taglib> </jsp-config>
5、jsp中使用该自定义标签
<%@ taglib prefix="test" uri="test" %>
<test:pagethird formName="pictureForm" showPages="${ pageBean.showPages }" curPage="${ pageBean.pageNo }" totalPages="${ pageBean.totalPages }"/>
6、over