JSP Custom Paging Labels

Source: Internet
Author: User
Tags tag name

1, the overall structure of the Web project


2. How to build TLD files in eclipse
3. Coding

Package Mytags;import Java.io.ioexception;import Javax.servlet.jsp.jspexception;import Javax.servlet.jsp.tagext.tagsupport;public class Pagertag extends TagSupport {/** * */private static final long serialver Sionuid = -3066943125964706748l;private String URL; form submission Address private int recordcount;//Total number of records private int pageno;//current page number private int pagesize;//page capacity//Label Body method @overridepublic int doStartTag () throws Jspexception {//TODO auto-generated method stub//Total pages int pagecount= (RECORDCOUNT+PAGESIZE-1)/ Pagesize;if (pageno>pagecount) {pageno=pagecount;} if (pageno<0) {pageno=1;} The form and script content submitted to the label body StringBuffer sb=new StringBuffer () sb.append ("<div class=\" pagination\ ">");//When action= "", Indicates that the page is not refreshed, only the contents of the form are updated sb.append ("<form action=\" \ "method=\" post\ "name=\" qpagerform\ "" >\r\n ");// Pass the implicit parameter sb.append of the current page number ("<input type=\" hidden\ "name=\" pageno\ "value="). Append (PageNo). Append (">");//Whether the previous page is displayed "Link if (pageno>1) {sb.append (" <a href=\ "javascript:void (null) \" onclick=\ "Turnoverpage (" + (pageNo-1) + ") \" > "); Sb.append ("Pre"); Sb.append ("</a>");} Displays the "..." before the current page, and other preceding display logic int start=1;if (pageno>4) {start=pageno-1;sb.append ("<a href=\" javascript:void ( NULL) \ "Onclick=\" turnoverpage (1) \ ">"), Sb.append ("1"); Sb.append ("</a>"); Sb.append ("<a href=\" Javascript:void (null) \ "Onclick=\" turnoverpage (2) \ ">") sb.append ("2"); Sb.append ("</a>"); Sb.append ("...") ;} int End=pageno+1;if (end>pagecount) {end=pagecount;} for (int i=start;i<=end;i++) {if (I==pageno) {sb.append ("<span class=\" current\ ">"). Append (PageNo). Append (" </span> ");} Else{sb.append ("<a href=\" javascript:void (null) \ "onclick=\" Turnoverpage ("). Append (i). Append (") \ ">"). Append (i). Append ("</a>");}} Display logic if (end<pagecount-2) {sb.append ("...") after the current page; if (end<pagecount-1) {sb.append ("<a href=\" javascript:void (null) \ "onclick=\" Turnoverpage ("). Append ( pageCount-1). Append (") \" > "). Append (PageCount-1). Append (" </a> ");} if (end<pagecount) {sb.append ("<a href=\" javascript:void (null) \"Onclick=\" Turnoverpage ("). Append (PageCount). Append (") \ ">"). Append (PageCount). Append ("</a>");//display "next page" "Link 2sb.append (" <a href=\ "javascript:void (null) \" onclick=\ "Turnoverpage ("). Append (pageno+1). Append (") \" > ") . Append ("Next"). Append ("</a>");} Sb.append ("</form></div>");//paging script function, main function://(1) Submit the jump page number to Testpagerservlet, update the content of the display//(2) Update the display of the paging label// The script function is implicit and does not display Sb.append ("<script language=\" javascript\ ">\r\n"); Sb.append ("Function turnoverpage (no) {\ r \ n") ;//The script is in the same document as the form, here is the method Sb.append ("var qform=document.qpagerform;\r\n") to get the form, Sb.append ("if (no>"). Append ( PageCount). Append (") {no="). Append (PageCount). Append (";} \ r \ n "); Sb.append (" if (no<1) {no=1;} \ r \ n "), Sb.append (" qform.pageno.value=no;\r\n "). Append (" Qform.action=\ "). Append (URL). Append (" \ ";"); Sb.append ("Qform.submit ();"). Append ("\ r \ n}"); Sb.append ("</script>"); try {pagecontext.getout (). println (Sb.tostring ());} catch ( IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();} return skip_body;} Public void setrecordcount (int num) {recordcount=num;} public void Setpageno (int num) {pageno=num;} public void setpagesize (int num) {pagesize=num;} public void SetUrl (String str) {url=str;}}
Import Java.io.ioexception;import java.util.arraylist;import java.util.list;import javax.servlet.ServletException; Import Javax.servlet.annotation.webservlet;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import javax.servlet.http.httpservletresponse;/** * servlet implementation Class Testpagertagservlet */@WebServlet ("/testpagertagservlet") public class Testpagertagservlet extends HttpServlet {    Private static final long serialversionuid = 1L;    private static final int page_size=10;    Private list<string> datas;        /** * @see httpservlet#httpservlet () */public Testpagertagservlet () {super (); TODO auto-generated Constructor stub}/** * @see Httpservlet#doget (httpservletrequest request, HttpServletResponse R esponse) */protected void doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated Method Stubdopost (request, response);} /** * @see HttpservleT#dopost (HttpServletRequest request, httpservletresponse response) */protected void DoPost (HttpServletRequest request , httpservletresponse response) throws Servletexception, IOException {//TODO auto-generated method Stubint pagesize= PAGE _size;request.setattribute ("PageSize", pageSize); int recordcount=datas.size ();//Gets the current page number string no= Request.getparameter ("PageNo"); int pageno=1;if (! "". Equals (NO) && no!=null) {pageno=integer.parseint (no);} Request.setattribute ("RecordCount", RecordCount); Request.setattribute ("PageNo", pageno); int s= (pageNo-1) *pagesize ; int E=s+pagesize;if (e>recordcount) {e=recordcount;} List<string> Subdatas=datas.sublist (S, e); Request.setattribute ("Subdatas", Subdatas); Request.getrequestdispatcher ("pager.jsp"). Forward (request, response);} @Overridepublic void Init () throws Servletexception {//TODO auto-generated method Stub//super.init ();//Generate test data Datas = N EW arraylist<string> (); for (int i=1;i<105;i++) {string str;str= "string" +i;datas.add (str);}}}
Pager.tdl

<pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "UTF-8"?><taglib> <tlib-version> 0.0</tlib-version> <jsp-version>0.0</jsp-version> <short-name>q</short-name> <!- -Uniform Resource Locator, unique-<uri>http://www.csdn.net/cjc/tags/pagertag</uri> <!--description of the custom label--><tag ><!--Tag name--><name>pager</name><!--the corresponding label processing class fully qualified name--><tag-class>mytags. pagertag</tag-class><!--The type of the label body--><body-content>empty</body-content><!--The description of the current page number property --><attribute><!--Property name--><name>pageno</name><!--whether the property is necessary--><required> true</required><!--whether the property value can dynamically produce--><rtexprvalue>true</rtexprvalue><!--the data type of the property at the time of the JSP runtime --><type>int</type></attribute><!--Description Information for the total number of records property--><attribute><name> recordcount</name><required>true</required><rtexprvalue>true</rtexprvalue><type>int</type></attribute><!--Description of the total page properties--><attribute><name>pagesize </name><required>true</required><rtexprvalue>true</rtexprvalue><type>int </type></attribute><!--Page Labels Description of the URI attribute to jump--><attribute><name>url</name> <required>true</required><rtexprvalue>true</rtexprvalue><type>java.lang.string </type></attribute></tag></taglib>

pager.jsp

<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "pageencoding=" iso-8859-1 "import =" java.util.* "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" ><%@ taglib Uri= "Http://www.csdn.net/cjc/tags/pagertag" prefix= "Q"%>Index.html

<! DOCTYPE html>

4. Results







JSP Custom Paging Labels

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.