1个回答
展开全部
给你代码看看啊;首先要编写一个javabean 叫
package sample.javaBean;
import sample.javaBean.ConnDb;
import java.util.List;
import java.util.ArrayList;
public class Paination {
private int currentPage;
private int totalPages;
private int pageRows=2;
private int totalRows;
private int pageStartRow;
private int pageEndRow;
private boolean hasPreviousPage;
private boolean hasNextPage;
private List<Object[]>totalList;
public void selectBySQL(){//这个方法主要是输出数据库里信息,就是你在servlet里把数据库信息放到list里一个道理:
List<Object[]> notes=new ArrayList<Object[]>();
ConnDb con=ConnDb.createInstance();
try{
String sql="select * from tb_type order by type_order";
con.setRs(con.query(sql));
while(con.getRs().next()){
Object[] note=new Object[5];
for(int i=0;i<5;i++){
note[i]=con.getRs().getObject(i+1);
}
notes.add(note);
}
con.getRs().close();
}catch (Exception e){
e.printStackTrace();
}
con.closeCon();
totalList=notes;
initPageBean(totalList,pageRows);
}
//分页的一个方法
public void initPageBean(List<Object[]>totalList,int pageRows){
this.totalList=totalList;
this.pageRows=pageRows;
this.totalRows=totalList.size();
this.currentPage=1;
if(totalRows%pageRows==0){
totalPages=totalRows/pageRows;
if(this.totalRows==0){
this.pageRows=0;
}
}else{
totalPages=totalRows/pageRows +1;
}
this.hasPreviousPage=false;
if(currentPage==totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
this.pageStartRow=1;
if(totalRows<pageRows){
this.pageEndRow=totalRows;
}else{
this.pageEndRow=pageRows;
}
}
//当前页的方法
public List<Object[]> getCurrentPageList(){
if(currentPage*pageRows<totalRows){
pageEndRow=currentPage*pageRows;
pageStartRow=pageEndRow-pageRows;
}else{
pageEndRow=totalRows;
pageStartRow=pageRows*(totalPages-1);
}
List<Object[]>pageList=new ArrayList<Object[]>(pageEndRow-pageStartRow+1);
if(totalRows!=0){
for(int i=pageStartRow;i<pageEndRow;i++){
pageList.add(totalList.get(i));
}
}
return pageList;
}
//上一页的一个方法
public List<Object[]> getPreviousPageList(){
currentPage=currentPage-1;
if(currentPage<1){
currentPage=1;
}
if(currentPage>=totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
if((currentPage-1)>0){
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
//下一页的一个方法
public List<Object[]> getNextPageList(){
currentPage=currentPage+1;
if(currentPage>totalPages){
currentPage=totalPages;
}
if((currentPage-1)>0){
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
if(currentPage>=totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
public List<Object[]> AppointPageList(int currentPage){
this.currentPage=currentPage;
if(currentPage>this.totalPages){
this.currentPage=this.totalPages;
}
if(currentPage<1){
this.currentPage=1;
}else{
hasPreviousPage=false;
}
if(currentPage>1){
this.hasPreviousPage=true;
}else{
this.hasPreviousPage=false;
}
if(this.currentPage<this.totalPages){
this.hasNextPage=true;
}else{
this.hasNextPage=false;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
public int getCurrentPage() {
return currentPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public int getPageEndRow() {
return pageEndRow;
}
public int getPageRows() {
return pageRows=1;
}
public int getPageStartRow() {
return pageStartRow;
}
public List<Object[]> getTotalList() {
return totalList;
}
public int getTotalPages() {
return totalPages;
}
public int getTotalRows() {
return totalRows;
}
}
下面是jsp里分页显示的代码:
<body>
<table align="center" border="1" height="150">
<jsp:useBean id="pa" class="sample.javaBean.Paination" scope="session"/>
<%
pa.selectBySQL();
List<Object[]> notes=pa.getCurrentPageList();
for(int m=0;m<notes.size();m++){
out.println("<tr align='center' bgcolor='#FFCC99'>");
Object[] note=notes.get(m);
for(int n=0;n<note.length;n++){
out.println("<td>"+note[n]+"</td>");
}
out.println("<br>");
out.println("</tr>");
}
%>
<form action="index.jsp" method="post" name="page" onsubmit="return checkPage(page)">
<tr align="right">
<td>
共有<font color="red" size="1"><%=pa.getTotalRows() %></font>条记录
当前是第<font color="red" size="1"><%=pa.getCurrentPage()+"/"+pa.getTotalPages() %></font>页
<%if(pa.isHasPreviousPage())
out.print("<a href='lunta/index2.jsp?requestPage=previousPage'>上一页</a>"); %>
<%if(pa.isHasNextPage())
out.print("<a href='lunta/index2.jsp?requestPage=nextPage'>下一页</a>"); %>
</td>
</tr>
</form>
</table>
</body>
下面是index2.jsp
<body>
<% Paination pa=(Paination)session.getAttribute("pa"); %>
<table align="center" border="1" height="30" width="500">
<%
List<Object[]> notes=new ArrayList<Object[]>();
String requestPage=request.getParameter("requestPage");
if(requestPage.equals("previousPage")){
notes =pa.getPreviousPageList();
}else if(requestPage.equals("nextPage")){
notes=pa.getNextPageList();
}
for(int m=0;m<notes.size();m++){
out.println("<tr align='center' height='30' bgcolor='#FFCC99'>");
Object[] note=notes.get(m);
for(int n=0;n<note.length;n++){
out.println("<td height='30'>"+note[n]+"</td>");
}
out.println("<br>");
out.println("</tr>");
}
%>
</table>
<table align="center" border="1" height="30" width="500">
<form action="index.jsp" method="post" name="page" onsubmit="return checkPage(page)">
<tr align="right" width="300">
<td>
共有<font color="red" size="1"><%=pa.getTotalRows() %></font>条记录
当前是第<font color="red" size="1"><%=pa.getCurrentPage()+"/"+pa.getTotalPages() %></font>页
<%if(pa.isHasPreviousPage())
out.print("<a href='lunta/index2.jsp?requestPage=previousPage'>上一页</a>"); %>
<%if(pa.isHasNextPage())
out.print("<a href='lunta/index2.jsp?requestPage=nextPage'>下一页</a>"); %>
第<input type="text" name="requestPage" size="3" maxlength="2">页
<input type="submit" value="转到>>">
</td>
</tr>
</form>
</table>
</body>
楼主细细分析,应该能看懂的啊!如果有必要你可以问我啊!
package sample.javaBean;
import sample.javaBean.ConnDb;
import java.util.List;
import java.util.ArrayList;
public class Paination {
private int currentPage;
private int totalPages;
private int pageRows=2;
private int totalRows;
private int pageStartRow;
private int pageEndRow;
private boolean hasPreviousPage;
private boolean hasNextPage;
private List<Object[]>totalList;
public void selectBySQL(){//这个方法主要是输出数据库里信息,就是你在servlet里把数据库信息放到list里一个道理:
List<Object[]> notes=new ArrayList<Object[]>();
ConnDb con=ConnDb.createInstance();
try{
String sql="select * from tb_type order by type_order";
con.setRs(con.query(sql));
while(con.getRs().next()){
Object[] note=new Object[5];
for(int i=0;i<5;i++){
note[i]=con.getRs().getObject(i+1);
}
notes.add(note);
}
con.getRs().close();
}catch (Exception e){
e.printStackTrace();
}
con.closeCon();
totalList=notes;
initPageBean(totalList,pageRows);
}
//分页的一个方法
public void initPageBean(List<Object[]>totalList,int pageRows){
this.totalList=totalList;
this.pageRows=pageRows;
this.totalRows=totalList.size();
this.currentPage=1;
if(totalRows%pageRows==0){
totalPages=totalRows/pageRows;
if(this.totalRows==0){
this.pageRows=0;
}
}else{
totalPages=totalRows/pageRows +1;
}
this.hasPreviousPage=false;
if(currentPage==totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
this.pageStartRow=1;
if(totalRows<pageRows){
this.pageEndRow=totalRows;
}else{
this.pageEndRow=pageRows;
}
}
//当前页的方法
public List<Object[]> getCurrentPageList(){
if(currentPage*pageRows<totalRows){
pageEndRow=currentPage*pageRows;
pageStartRow=pageEndRow-pageRows;
}else{
pageEndRow=totalRows;
pageStartRow=pageRows*(totalPages-1);
}
List<Object[]>pageList=new ArrayList<Object[]>(pageEndRow-pageStartRow+1);
if(totalRows!=0){
for(int i=pageStartRow;i<pageEndRow;i++){
pageList.add(totalList.get(i));
}
}
return pageList;
}
//上一页的一个方法
public List<Object[]> getPreviousPageList(){
currentPage=currentPage-1;
if(currentPage<1){
currentPage=1;
}
if(currentPage>=totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
if((currentPage-1)>0){
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
//下一页的一个方法
public List<Object[]> getNextPageList(){
currentPage=currentPage+1;
if(currentPage>totalPages){
currentPage=totalPages;
}
if((currentPage-1)>0){
hasPreviousPage=true;
}else{
hasPreviousPage=false;
}
if(currentPage>=totalPages){
hasNextPage=false;
}else{
hasNextPage=true;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
public List<Object[]> AppointPageList(int currentPage){
this.currentPage=currentPage;
if(currentPage>this.totalPages){
this.currentPage=this.totalPages;
}
if(currentPage<1){
this.currentPage=1;
}else{
hasPreviousPage=false;
}
if(currentPage>1){
this.hasPreviousPage=true;
}else{
this.hasPreviousPage=false;
}
if(this.currentPage<this.totalPages){
this.hasNextPage=true;
}else{
this.hasNextPage=false;
}
List<Object[]> pageList=this.getCurrentPageList();
return pageList;
}
public int getCurrentPage() {
return currentPage;
}
public boolean isHasNextPage() {
return hasNextPage;
}
public boolean isHasPreviousPage() {
return hasPreviousPage;
}
public int getPageEndRow() {
return pageEndRow;
}
public int getPageRows() {
return pageRows=1;
}
public int getPageStartRow() {
return pageStartRow;
}
public List<Object[]> getTotalList() {
return totalList;
}
public int getTotalPages() {
return totalPages;
}
public int getTotalRows() {
return totalRows;
}
}
下面是jsp里分页显示的代码:
<body>
<table align="center" border="1" height="150">
<jsp:useBean id="pa" class="sample.javaBean.Paination" scope="session"/>
<%
pa.selectBySQL();
List<Object[]> notes=pa.getCurrentPageList();
for(int m=0;m<notes.size();m++){
out.println("<tr align='center' bgcolor='#FFCC99'>");
Object[] note=notes.get(m);
for(int n=0;n<note.length;n++){
out.println("<td>"+note[n]+"</td>");
}
out.println("<br>");
out.println("</tr>");
}
%>
<form action="index.jsp" method="post" name="page" onsubmit="return checkPage(page)">
<tr align="right">
<td>
共有<font color="red" size="1"><%=pa.getTotalRows() %></font>条记录
当前是第<font color="red" size="1"><%=pa.getCurrentPage()+"/"+pa.getTotalPages() %></font>页
<%if(pa.isHasPreviousPage())
out.print("<a href='lunta/index2.jsp?requestPage=previousPage'>上一页</a>"); %>
<%if(pa.isHasNextPage())
out.print("<a href='lunta/index2.jsp?requestPage=nextPage'>下一页</a>"); %>
</td>
</tr>
</form>
</table>
</body>
下面是index2.jsp
<body>
<% Paination pa=(Paination)session.getAttribute("pa"); %>
<table align="center" border="1" height="30" width="500">
<%
List<Object[]> notes=new ArrayList<Object[]>();
String requestPage=request.getParameter("requestPage");
if(requestPage.equals("previousPage")){
notes =pa.getPreviousPageList();
}else if(requestPage.equals("nextPage")){
notes=pa.getNextPageList();
}
for(int m=0;m<notes.size();m++){
out.println("<tr align='center' height='30' bgcolor='#FFCC99'>");
Object[] note=notes.get(m);
for(int n=0;n<note.length;n++){
out.println("<td height='30'>"+note[n]+"</td>");
}
out.println("<br>");
out.println("</tr>");
}
%>
</table>
<table align="center" border="1" height="30" width="500">
<form action="index.jsp" method="post" name="page" onsubmit="return checkPage(page)">
<tr align="right" width="300">
<td>
共有<font color="red" size="1"><%=pa.getTotalRows() %></font>条记录
当前是第<font color="red" size="1"><%=pa.getCurrentPage()+"/"+pa.getTotalPages() %></font>页
<%if(pa.isHasPreviousPage())
out.print("<a href='lunta/index2.jsp?requestPage=previousPage'>上一页</a>"); %>
<%if(pa.isHasNextPage())
out.print("<a href='lunta/index2.jsp?requestPage=nextPage'>下一页</a>"); %>
第<input type="text" name="requestPage" size="3" maxlength="2">页
<input type="submit" value="转到>>">
</td>
</tr>
</form>
</table>
</body>
楼主细细分析,应该能看懂的啊!如果有必要你可以问我啊!
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询