jsp+javaBean实现分页

/*
 * PageNumber.java
 *
 * Created on 2006年10月9日, 上午8:21
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package page;
import java.io.*;
import java.sql.*;
import java.lang.String;
/**
 *只需传进三个参数,一个是每页显示记录集数,一个是记录集,一个是URL
 *取得分页效果只需调用一个getPage即可
 * @mc1035
 */
public class PageNumber {
    private int rowCount=1,   //总的记录数。
            pageSize=1,  //每页显示的记录数。
            showPage=1,  //设置欲显示的页码数。
            pageCount=1; //分页之后的总页数。

    private ResultSet rs;
    private String url;
    private StringBuffer page=new StringBuffer();
    private String formValue="-1";
   
    /*
     *设置记录集
     */
    public void setRs(ResultSet a){
        rs=a;
        try {
            rs.last(); //获取最后一行的行号:
            rowCount=rs.getRow();//获取
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
       
    }
    public ResultSet getRs(){
        return rs;
    }
    /*
     *设置url
     */
    public void setUrl(String a){
        url=a;
    }
    public String getUrl(){
        return url;
    }
    /*
     *设置总记录数
     */
    public void setRowCount(int n) {
        rowCount=n;
    }
    public int getRowCount() {
        return rowCount;
    }
   
    /*
     *设置总的页数
     */
    public void setPageCount() {
       
        int n=(rowCount%pageSize)==0?(rowCount/pageSize):(rowCount/pageSize+1) ;
        pageCount=n;
    }
    public int getPageCount() {
        return pageCount;
    }
    /*
     *设置显示第几页
     */
    public void setShowPage(int n) {
        showPage=n;
    }
    public int getShowPage() {
        return showPage;
    }
    /*
     *每页显示的记录数
     */
    public void setPageSize(int n) {
        pageSize=n;
       
    }
    public int getPageSize() {
        return pageSize;
    }
    /*
     *设置表单值
     */
    public void setFormValue(String a){
        formValue=a;
    }
    public String getFormValue(){
        return formValue;
    }
    public void setForm(){
       
        page.append("共有"+getRowCount()+"条记录 ");
        page.append("  "+getPageCount()+"页 ");
        page.append(" 当前为第"+getShowPage()+"页 ");
        page.append("每页显示"+getPageSize()+"条记录");
       
        if(getShowPage()!=1){
            page.append("<Form action=/""+url+"/" method=/"post/">");
            page.append("<Input Type=/"hidden/" name=/"formValue/" value=/"1/">");
            page.append("<Input name=/"submit4/" type=/"submit/" value=/"首页/">");
            page.append("</FORM>");
        }else{
            page.append("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
        }
        if(getShowPage()!=getPageCount()){
            page.append("<Form action=/""+url+"/" method=/"post/">");
            page.append("<Input Type=/"hidden/" name=/"formValue/" value=/""+(getShowPage()+1)+"/">");
           page.append("<Input name=/"submit4/" type=/"submit/" value=/"下一页/">");
            page.append("</FORM>");
        }else{
        page.append("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
        }
        if(getShowPage()!=1){
            page.append("<Form action=/""+url+"/" method=/"post/">");
            page.append("<Input Type=/"hidden/" name=/"formValue/" value=/""+(getShowPage()-1)+"/">");
          
           page.append("<Input name=/"submit4/" type=/"submit/" value=/"上一页/">");
            page.append("</FORM>");
        }else{
            page.append("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
        }
        if(getShowPage()!=getPageCount()){
            page.append("<Form action=/""+url+"/" method=/"post/">");
            page.append("<Input Type=/"hidden/" name=/"formValue/" value=/""+getPageCount()+"/">");
           
           page.append("<Input name=/"submit4/" type=/"submit/" value=/"末页/">");
            page.append("</FORM>");
        }else{
            page.append("&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp");
        }
       
        page.append("<Form action=/""+url+"/" method=/"post/">");
        page.append("输入欲要显示的页<Input Type=/"text/" name=/"formValue/" size=/"5/" value=/""+getShowPage()+"/">");
      
        page.append("<Input name=/"submit4/" type=/"submit/" value=/"GO/">");
        page.append("</FORM>");
       
       
    }
    public void showList(){
        try{
            page.append("<Table>");
            page.append("<TR>");
            ResultSetMetaData statName= rs.getMetaData();
           
            for(int i=1;i<=statName.getColumnCount();i++){
                page.append("<TH>"+statName.getColumnName(i));
            }
            page.append("</TR>");
           
            for(int i=1;i<=getPageSize();i++) {
                page.append("<TR>");
                for(int j=1;j<=statName.getColumnCount();j++){
                    page.append("<TD>"+rs.getString(j)+"</TD>");
                }
                page.append("</TR>") ;
                if(!rs.next()){
                    break;
                }
            }
            page.append("</Table>");
            setForm();
           
        }catch(Exception e){
            e.printStackTrace();
            page.append(e);
        }
    }
    public String getPage(){
        setPageCount();
        try{
            int n=1;
            if(formValue.equals("-1")){
                formValue="1";
                setShowPage(1);
                rs.absolute((n-1)*getPageSize()+1);
               
            }else {
                try{
                    n=Integer.parseInt(formValue);
                    if(n>getPageCount()) {
                        n=getPageCount();
                    }
                    if(n<=0) {
                        n=1;
                    }
                    setShowPage(n);
                    rs.absolute((n-1)*getPageSize()+1);
                   
                } catch(NumberFormatException e){
                    e.printStackTrace();
                    n=getShowPage();
                    rs.absolute((n-1)*getPageSize()+1);
                }
            }
        } catch(SQLException ex){
            ex.printStackTrace();
        }
        showList();
       
       
        String a=new String(page);
        return a;
    }
   
   
}

//index.jap文件,引用例子

 <%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*"%>
<%@page import="page.PageNumber"%>
<%@page import="java.io.*"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <jsp:useBean id="pageNum" scope="page" class="page.PageNumber" />
   
        <%!
            Connection con=null;
            Statement sql=null;
            ResultSet rs=null;

            int rowCount=0;
            int pageSize=5;//每页显示的记录集数
        %>
        <%      try{

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con = DriverManager.getConnection("jdbc:odbc:mcpage");
                sql=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
                rs=sql.executeQuery("select * from user");

        %>
        <% //URL
            String str=response.encodeRedirectURL("index.jsp");

        //下面的是Bean的参数,可根据需要自行修改
        %> 
     
        <jsp:setProperty name="pageNum" property="formValue" param="formValue"/>     
        <jsp:setProperty name="pageNum" property="rs" value="<%=rs%>"/>
        <jsp:setProperty name="pageNum" property="pageSize" value="<%=pageSize%>"/>
        <jsp:setProperty name="pageNum" property="url" value="<%=str%>"/>       
        <jsp:getProperty name="pageNum" property="page"/>
      
        <%

            rs.close();
            con.close();
            }catch(SQLException e){
                e.printStackTrace();
                out.print(e);
                rs.close();
                con.close();
            }
        %>
   
    </body>
</html>

配置好执行后会实现分页就是样式少差一点,呵呵,时间匆忙,以后会加以改进的.

你可能感兴趣的:(jsp,String,url,input,action,import)