A simple implementation of paging (mysql)

Source: Internet
Author: User

Mybook. jsp

 

The original session. currentPage can be implemented in jsp mode or in the event message HttpSessionListener.

<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8 "<br/> pageencoding =" UTF-8 "%> <br/> <% @ taglib uri ="/WEB-INF/why. TLD "prefix =" myexample "%> <br/> <! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd"> <br/> <HTML> <br/> <pead> <br/> <MCE: script Type = "text/JavaScript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" mce_src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"> </MCE: SCRIPT> <br/> <MCE: script Type = "text/JavaScript" src = "http: // localhost/show/JS/you. JS" Mce_src = "http: // localhost/show/JS/you. JS "> </MCE: SCRIPT> <br/> <LINK rel =" stylesheet "<br/> href =" http: // localhost/show/CSS/you.css "mce_href =" http: // localhost/show/CSS/you.css "<br/> type =" text/CSS "/> <br/> <meta http-equiv =" Content-Type "content = "text/html; charset = UTF-8 "> <br/> <title> insert title here </title> <br/> </pead> <br/> <body> <br/> <myexample: to here = "Do I"/> <br/> <Div class = "change"> <br /> <Myexample: Query table = "book" page = "$ {sessionscope. currentpage = NULL? 1: sessionscope. currentpage} "/> <br/> </div> <br/> </body> <br/> </ptml> 

You. js

 

$ (Document ). ready (function () {<br/> $ ("span. click "). click (function () {<br/> var XMLHTTP; <br/> If (window. XMLHttpRequest) <br/>{// code for IE7 +, Firefox, chrome, opera, Safari <br/> XMLHTTP = new XMLHttpRequest (); <br/>}< br/> else <br/> {// code for IE6, ie5 <br/> XMLHTTP = new activexobject ("Microsoft. XMLHTTP "); <br/>}< br/> XMLHTTP. onreadystatechange = function () <br/>{< br/> If (XMLHTTP. readysta Te = 4 & XMLHTTP. status = 200) <br/>{< br/> location. reload (); <br/>}< br/>}; <br/> XMLHTTP. open ("get ",". /change? Page = "commandid (this).html (), true); <br/> XMLHTTP. send (); </P> <p >}); <br/> 

 

Implemented Using custom tags,

Tag


Package my; <br/> import java. io. IOException; <br/> import java. SQL. *; <br/> import javax. servlet. jsp. tagext. *; <br/> public class Query extends SimpleTagSupport {<br/> private Statement statement; <br/> private String table = null; <br/> private int page, number = 2; <br/> private ResultSet resultSet; <br/> private String follow = ""; <br/> public void doTag () {<br/> String querySql = "SELECT name, price FROM" + tabl E; <br/> try {<br/> statement = MySql. getStatement ("why", "root", "root"); <br/> resultSet=statement.exe cuteQuery (querySql); <br/> this. getJspContext (). getOut (). println ("<br/>" + "<br/>" + getResult (); <br/>} catch (Exception e) {<br/> // TODO Auto-generated catch block <br/> e. printStackTrace (); <br/>}< br/> private String getResult () throws Exception {<br/> int totalRow, totalPage; <br/> String s Tr = null; <br/> if (page-1) = 0 | resultSet. absolute (page-1) * number) & resultSet. last () {<br/> totalRow = resultSet. getRow (); <br/> totalPage = totalRow/number + (totalRow % number = 0? 0: 1); <br/> if (page-1> 0) <br/> resultSet. absolute (page-1) * number); <br/> else <br/> resultSet. beforeFirst (); <br/>}else {<br/> follow + = "null"; <br/> return null; <br/>}</p> <p> for (int I = 0; I <number & resultSet. next (); ++ I) {<br/> str + = "<div class =/" red/">" + resultSet. getString ("name") + "<br/>" + resultSet. getDouble ("price") + "</div> <br/>"; <br/> follow + = resultSet. getString ("name") + resultSet. getDouble ("price"); <br/>}< br/> for (int I = 1; I <= totalPage; ++ I) {<br/> if (I = page) {<br/> str + = "<span style ="/"mce_style ="/"" color: red/">" + I + "</span>"; <br/> continue; <br/>}< br/> str + = "<span class =/" click/">" + I + "</span> "; <br/>}< br/> return str; <br/>}< br/> public void setTable (String table) {<br/> this. table = table; <br/>}< br/> public void setPage (int page) {<br/> this. page = page; <br/>}< br/> public void setNumber (int number) {<br/> this. number = number; <br/>}< br/>} 

 

Why. tld

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <taglib xmlns = "http://java.sun.com/xml/ns/j2ee" <br/> xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" <br/> xsi: schemalocation = "http://java.sun.com/xml/ns/j2ee <br/> http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd" <br/> Version = "2.0"> </P> <p> <tlib-version> 1.0 </tlib-version> <br/> <short-Name> Why </short-Name> <! -- My. example declaration name example --> <br/> <tag> <br/> <Name> to </Name> <br/> <tag-class> my. example </Tag-class> <br/> <body-content> Empty </body-content> <br/> <attribute> <br/> <Name> here </ name> <br/> </attribute> <br/> </Tag> <br/> <tag> <br/> <Name> query </Name> <br/> <tag-class> my. query </Tag-class> <br/> <body-content> Empty </body-content> <br/> <attribute> <br/> <Name> table </ name> <br/> <required> true </required> <br/> </attribute> <br/> <Name> page </Name> <br/> <required> true </required> <br/> <rtexprvalue> true </rtexprvalue> <br/> </attribute> <br/> <attribute> <br/> <name> Number </Name> <br/> </attribute> <br/> </Tag> </P> <p> </taglib> 

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.