一個簡單的分頁(mysql) 簡單實現

來源:互聯網
上載者:User

mybook.jsp

 

對於原始session.currentPage可以再jsp種實現 也可以在 事件訊息 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 /><head><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 /></head><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 /></html> 

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.readyState==4 && xmlhttp.status==200)<br /> {<br /> location.reload();<br /> }<br /> };<br /> xmlhttp.open("GET","./change?page="+$(this).html(),true);<br /> xmlhttp.send();</p><p> });<br />});<br /> 

 

我使用自訂標籤實現的,

標籤


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 "+table;<br />try {<br />statement =MySql.getStatement("why", "root", "root");<br />resultSet=statement.executeQuery(querySql);<br /> this.getJspContext().getOut().println("<br/>"+"<br/>"+getResult());<br />} catch (Exception e){<br />// TODO Auto-generated catch block<br />e.printStackTrace();<br /> }<br />}<br /> private String getResult() throws Exception{<br /> int totalRow,totalPage;<br /> String str = 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 聲明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 /> <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> 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.