分页显示是WEB开发中都要用到的一种数据显示方法,这里介绍一种差不多全靠配置来完成分页显示功能的方式,运用这种方式来完成数据分页显示,你会发现原来分页也可以做到如此简单,不用再写繁琐的JSP代码,你只需更改几个特定参数就可以随心所欲地进行分页。本例大量采用JSTL标签,代码简单整齐排列美观,读者不妨一试!
WEB服务器:Tomcat
数据库:Microsoft SQL Server 2000
值得说明的一点是,在代码较多的地方,有可能我不会将代码全部列出来,而只是挑选关键地方加以说明,而后我会将完整代码上传到附件中,读者朋友有不懂的地方可以参照附件。
为节省篇幅,数据类DBConnect我就不列出代码,其功能就是根据传入的连接池名来完成数据库连接及数据库查询和更新操作,有需要读者朋友可以参考附件,下面提一下所用到的连接池为proxool,而后只要将连接池别名(本例中为bpDbCon)传入连接类DBConnect即可实现连接和其它数据操作。
<?xml version="1.0" encoding="UTF-8"?>
<something-else-entirely>
<proxool>
<alias>bpDbCon</alias>
<driver-url>jdbc:microsoft:sqlserver://192.168.39.62:1433;DatabaseName=bpdb</driver-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<driver-properties>
<property name="user" value="test"/>
<property name="password" value="test"/>
<property name="useUnicode" value="true"/>
</driver-properties>
<minimum-connection-count>6</minimum-connection-count>
<maximum-connection-count>20</maximum-connection-count>
<prototype-count>6</prototype-count>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
</something-else-entirely>
<something-else-entirely>
<proxool>
<alias>bpDbCon</alias>
<driver-url>jdbc:microsoft:sqlserver://192.168.39.62:1433;DatabaseName=bpdb</driver-url>
<driver-class>com.microsoft.jdbc.sqlserver.SQLServerDriver</driver-class>
<driver-properties>
<property name="user" value="test"/>
<property name="password" value="test"/>
<property name="useUnicode" value="true"/>
</driver-properties>
<minimum-connection-count>6</minimum-connection-count>
<maximum-connection-count>20</maximum-connection-count>
<prototype-count>6</prototype-count>
<house-keeping-test-sql>select CURRENT_DATE</house-keeping-test-sql>
</proxool>
</something-else-entirely>
1,数据表结构,这个是我用到的数据表。
CREATE TABLE [dbo].[recordsList] (
[nId] [int] IDENTITY (1, 1) NOT NULL ,
[strPoNo] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strGrNo] [nvarchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[nItem] [int] NOT NULL ,
[strMaterialDesc] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[strMaterialNo] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strMaterialName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strApplicant] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[fltUnitPrice] [float] NULL ,
[nCount] [int] NOT NULL ,
[strDate] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strDeptName] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strSignDate] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[strMaterialSort] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
[nId] [int] IDENTITY (1, 1) NOT NULL ,
[strPoNo] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strGrNo] [nvarchar] (30) COLLATE Chinese_PRC_CI_AS NULL ,
[nItem] [int] NOT NULL ,
[strMaterialDesc] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NULL ,
[strMaterialNo] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strMaterialName] [nvarchar] (100) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strApplicant] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NULL ,
[fltUnitPrice] [float] NULL ,
[nCount] [int] NOT NULL ,
[strDate] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strDeptName] [nvarchar] (20) COLLATE Chinese_PRC_CI_AS NOT NULL ,
[strSignDate] [nvarchar] (10) COLLATE Chinese_PRC_CI_AS NULL ,
[strMaterialSort] [nvarchar] (50) COLLATE Chinese_PRC_CI_AS NULL
) ON [PRIMARY]
2,要用到的分页存储过程P_viewPage,这个存储过程在整个分页显示中起了关键作用,靠传入的参数来获得所需要的记录集
CREATE PROC P_viewPage
/*
no_mIss 分页存储过程 2007.2.20 QQ:706716259
适用于单一主键或存在唯一值列的表或视图
*/
@TableName VARCHAR(200), --表名
@FieldList VARCHAR(2000), --显示列名
@PrimaryKey VARCHAR(100), --单一主键或唯一值键
@Where VARCHAR(1000), --查询条件 不含'where'字符
@Order VARCHAR(1000), --排序 不含'order by'字符,如id asc,userid desc,当@SortType=3时生效
@SortType INT, --排序规则 1:正序asc 2:倒序desc 3:多列排序
@RecorderCount INT, --记录总数 0:会返回总记录
@PageSize INT, --每页输出的记录数
@PageIndex INT, --当前页数
@TotalCount INTEGER OUTPUT, --返回记录总数
@TotalPageCount INTEGER OUTPUT --返回总页数
AS
SET NOCOUNT ON
IF ISNULL(@TableName,'') = '' OR ISNULL(@FieldList,'') = ''
OR ISNULL(@PrimaryKey,'') = ''
OR @SortType < 1 OR @SortType >3
OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
BEGIN
RETURN
END
DECLARE @new_where1 VARCHAR(1000)
DECLARE @new_where2 VARCHAR(1000)
DECLARE @new_order VARCHAR(1000)
DECLARE @Sql VARCHAR(8000)
DECLARE @SqlCount NVARCHAR(4000)
IF ISNULL(@where,'') = ''
BEGIN
SET @new_where1 = ' '
SET @new_where2 = ' WHERE '
END
ELSE
BEGIN
SET @new_where1 = ' WHERE ' + @where
SET @new_where2 = ' WHERE ' + @where + ' AND '
END
IF ISNULL(@order,'') = '' OR @SortType = 1 OR @SortType = 2
BEGIN
IF @SortType = 1 SET @new_order = ' ORDER BY ' + @PrimaryKey + ' ASC'
IF @SortType = 2 SET @new_order = ' ORDER BY ' + @PrimaryKey + ' DESC'
END
ELSE
BEGIN
SET @new_order = ' ORDER BY ' + @Order
END
SET @SqlCount = 'SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'
+ CAST(@PageSize AS VARCHAR)+') FROM ' + @TableName + @new_where1
IF @RecorderCount = 0
BEGIN
EXEC SP_EXECUTESQL @SqlCount,N'@TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT',
@TotalCount OUTPUT,@TotalPageCount OUTPUT
END
ELSE
BEGIN
SELECT @TotalCount = @RecorderCount
END
IF @PageIndex > CEILING((@TotalCount+0.0)/@PageSize)
BEGIN
SET @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)
--这里加一行测试
--print '当前页PageIndex为'+str(@PageIndex)
END
IF @PageIndex = 0 or @PageIndex = 1
--IF @PageIndex = 1 --原始行
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where1 + @new_order
END
ELSE
BEGIN
IF @SortType = 1
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' > '
+ '(SELECT max(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order +' ) AS TMP) '+ @new_order
END
IF @SortType = 2
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' < '
+ '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+' FROM '+ @TableName
+ @new_where1 + @new_order + ') AS TMP) '+ @new_order
END
IF @SortType = 3
BEGIN
IF CHARINDEX(',',@Order) = 0 BEGIN RETURN END
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' NOT IN (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+ ' FROM ' + @TableName + @new_where1 + @new_order + ')'
+ @new_order
END
END
EXEC(@Sql)
--print @Sql
/*
no_mIss 分页存储过程 2007.2.20 QQ:706716259
适用于单一主键或存在唯一值列的表或视图
*/
@TableName VARCHAR(200), --表名
@FieldList VARCHAR(2000), --显示列名
@PrimaryKey VARCHAR(100), --单一主键或唯一值键
@Where VARCHAR(1000), --查询条件 不含'where'字符
@Order VARCHAR(1000), --排序 不含'order by'字符,如id asc,userid desc,当@SortType=3时生效
@SortType INT, --排序规则 1:正序asc 2:倒序desc 3:多列排序
@RecorderCount INT, --记录总数 0:会返回总记录
@PageSize INT, --每页输出的记录数
@PageIndex INT, --当前页数
@TotalCount INTEGER OUTPUT, --返回记录总数
@TotalPageCount INTEGER OUTPUT --返回总页数
AS
SET NOCOUNT ON
IF ISNULL(@TableName,'') = '' OR ISNULL(@FieldList,'') = ''
OR ISNULL(@PrimaryKey,'') = ''
OR @SortType < 1 OR @SortType >3
OR @RecorderCount < 0 OR @PageSize < 0 OR @PageIndex < 0
BEGIN
RETURN
END
DECLARE @new_where1 VARCHAR(1000)
DECLARE @new_where2 VARCHAR(1000)
DECLARE @new_order VARCHAR(1000)
DECLARE @Sql VARCHAR(8000)
DECLARE @SqlCount NVARCHAR(4000)
IF ISNULL(@where,'') = ''
BEGIN
SET @new_where1 = ' '
SET @new_where2 = ' WHERE '
END
ELSE
BEGIN
SET @new_where1 = ' WHERE ' + @where
SET @new_where2 = ' WHERE ' + @where + ' AND '
END
IF ISNULL(@order,'') = '' OR @SortType = 1 OR @SortType = 2
BEGIN
IF @SortType = 1 SET @new_order = ' ORDER BY ' + @PrimaryKey + ' ASC'
IF @SortType = 2 SET @new_order = ' ORDER BY ' + @PrimaryKey + ' DESC'
END
ELSE
BEGIN
SET @new_order = ' ORDER BY ' + @Order
END
SET @SqlCount = 'SELECT @TotalCount=COUNT(*),@TotalPageCount=CEILING((COUNT(*)+0.0)/'
+ CAST(@PageSize AS VARCHAR)+') FROM ' + @TableName + @new_where1
IF @RecorderCount = 0
BEGIN
EXEC SP_EXECUTESQL @SqlCount,N'@TotalCount INT OUTPUT,@TotalPageCount INT OUTPUT',
@TotalCount OUTPUT,@TotalPageCount OUTPUT
END
ELSE
BEGIN
SELECT @TotalCount = @RecorderCount
END
IF @PageIndex > CEILING((@TotalCount+0.0)/@PageSize)
BEGIN
SET @PageIndex = CEILING((@TotalCount+0.0)/@PageSize)
--这里加一行测试
--print '当前页PageIndex为'+str(@PageIndex)
END
IF @PageIndex = 0 or @PageIndex = 1
--IF @PageIndex = 1 --原始行
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where1 + @new_order
END
ELSE
BEGIN
IF @SortType = 1
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' > '
+ '(SELECT max(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+ ' FROM ' + @TableName
+ @new_where1 + @new_order +' ) AS TMP) '+ @new_order
END
IF @SortType = 2
BEGIN
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' < '
+ '(SELECT MIN(' + @PrimaryKey + ') FROM (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+' FROM '+ @TableName
+ @new_where1 + @new_order + ') AS TMP) '+ @new_order
END
IF @SortType = 3
BEGIN
IF CHARINDEX(',',@Order) = 0 BEGIN RETURN END
SET @Sql = 'SELECT TOP ' + STR(@PageSize) + ' ' + @FieldList + ' FROM '
+ @TableName + @new_where2 + @PrimaryKey + ' NOT IN (SELECT TOP '
+ STR(@PageSize*(@PageIndex-1)) + ' ' + @PrimaryKey
+ ' FROM ' + @TableName + @new_where1 + @new_order + ')'
+ @new_order
END
END
EXEC(@Sql)
--print @Sql
3,分页显示类,两个,父类Pagess,子类Listss,这两个类接收Jsp页面传来的参数,并将参数整理后传给所调用的分页存储过程,同时将存储过程返回的记录集结果封装起来
父类Pages
package sqlDBconnect;
import java.util.Vector;
/**
* Title: 分页类
* Description:
* Copyright: Copyright (c) 2007
* Company: USI
* @author: LJ
* @version 1.0
*/
public class Pagess {
protected Vector list; //物品向量数组
protected int page=1; //当前页数
protected int recordCount=0; //记录总数 0:会返回总记录
protected int pageCount=1; //返回总页数
protected int pageSize=15; //每页输出的记录数
protected int recode=0;
protected int pageMaxCount=0;
protected int listCount=0;
protected boolean showAll=false;
public Pagess() throws Exception{
}
public void setPage(int newPage) {
this.page=newPage;
}
public int getPage() {
if (page<=0)
return 1;
else
return page;
}
public void setPageSize(int newPageSize) {
pageSize=newPageSize;
}
public void setShowAll() {
showAll=true;
}
public int getPageSize() {
return pageSize;
}
public int getPageCount() {
return pageCount;
}
public int getRecordCount() {
return recordCount;
}
public int getRecode() {
return recode;
}
public void setListCount(int listCount){
this.listCount=listCount;
}
public int getListCount() {
return list.size();
}
public Vector getLists() {
return list;
}
public void setShowPage(int newCount) throws Exception {
listCount=newCount;
list = new Vector();//PageMaxCount+1);
list.clear();
recode=(page-1)*pageSize+1;
}
/**
* @param pageCount 要设置的 pageCount
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
/**
* @param recordCount 要设置的 recordCount
*/
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
};
import java.util.Vector;
/**
* Title: 分页类
* Description:
* Copyright: Copyright (c) 2007
* Company: USI
* @author: LJ
* @version 1.0
*/
public class Pagess {
protected Vector list; //物品向量数组
protected int page=1; //当前页数
protected int recordCount=0; //记录总数 0:会返回总记录
protected int pageCount=1; //返回总页数
protected int pageSize=15; //每页输出的记录数
protected int recode=0;
protected int pageMaxCount=0;
protected int listCount=0;
protected boolean showAll=false;
public Pagess() throws Exception{
}
public void setPage(int newPage) {
this.page=newPage;
}
public int getPage() {
if (page<=0)
return 1;
else
return page;
}
public void setPageSize(int newPageSize) {
pageSize=newPageSize;
}
public void setShowAll() {
showAll=true;
}
public int getPageSize() {
return pageSize;
}
public int getPageCount() {
return pageCount;
}
public int getRecordCount() {
return recordCount;
}
public int getRecode() {
return recode;
}
public void setListCount(int listCount){
this.listCount=listCount;
}
public int getListCount() {
return list.size();
}
public Vector getLists() {
return list;
}
public void setShowPage(int newCount) throws Exception {
listCount=newCount;
list = new Vector();//PageMaxCount+1);
list.clear();
recode=(page-1)*pageSize+1;
}
/**
* @param pageCount 要设置的 pageCount
*/
public void setPageCount(int pageCount) {
this.pageCount = pageCount;
}
/**
* @param recordCount 要设置的 recordCount
*/
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
};
子类Listss,注意其中的excute方法,该方法就是用来根据传入的参数返回所需记录集,并将结果记录集封装以便在jsp页面更容易地调用。
package sqlDBconnect;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
/**
* Title: 物品列表
* Description:
* Copyright: Copyright (c) 2007
* Company: USI
* @author: LJ
* @version 1.0
*/
public class Listss extends Pagess {
private String[] columnNames = {};
private ResultSetMetaData metaData;
int row=0;
int columnCount=0;
String strSql="";
int execute=0;
String sql="";
private String poolName;
private String tableName="";
private String fieldList="";
private String primaryKey="";
private String where="";
private String order="";
private int sortType=1;
/**
* @return fieldList
*/
public String getFieldList() {
return fieldList;
}
/**
* @param fieldList 要设置的 fieldList
*/
public void setFieldList(String fieldList) {
this.fieldList = fieldList;
}
/**
* @return order
*/
public String getOrder() {
return order;
}
/**
* @param order 要设置的 order
*/
public void setOrder(String order) {
this.order = order;
}
/**
* @return primaryKey
*/
public String getPrimaryKey() {
return primaryKey;
}
/**
* @param primaryKey 要设置的 primaryKey
*/
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
/**
* @return sortType
*/
public int getSortType() {
return sortType;
}
/**
* @param sortType 要设置的 sortType
*/
public void setSortType(int sortType) {
this.sortType = sortType;
//System.out.println(this.sortType);
}
/**
* @return tableName
*/
public String getTableName() {
return tableName;
}
/**
* @param tableName 要设置的 tableName
*/
public void setTableName(String tableName) {
this.tableName = tableName;
}
/**
* @return where
*/
public String getWhere() {
return where;
}
/**
* @param where 要设置的 where
*/
public void setWhere(String where) {
this.where = where;
}
public Listss() throws Exception{
super();
}
public String getColumnName(int column) {
if (column>=columnCount)
column=columnCount-1;
if (column<0)
column=0;
if (columnNames[column] != null) {
return columnNames[column];
} else
return "";
}
public int getColumnIndex(String name) {
int ok=0;
if (name!=null) {
for (int i=0;i<columnCount;i++) {
String temp=columnNames[i].toLowerCase();
name=name.toLowerCase();
if (name.equals(temp))
{
ok= i;
break;
}
}
}
return ok;
}
public void setRow(int i) {
row=i;
}
public int getRow(){
return this.row;
}
public int getColumnCount() {
return columnNames.length;
}
public Class getColumnClass(int column) {
int type;
try {
type = metaData.getColumnType(column+1);
}
catch (SQLException e) {
return null;
}
switch(type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class;
case Types.BIT:
return Boolean.class;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class;
case Types.BIGINT:
return Long.class;
case Types.FLOAT:
case Types.DOUBLE:
return Double.class;
case Types.DATE:
return java.sql.Date.class;
default:
return Object.class;
}
}
public String getString(int aColumn) {
String[] rowStr = (String[])list.elementAt(row);
return rowStr[aColumn];
// Vector row = (Vector)list.elementAt(FRow);
// return row.elementAt(aColumn).toString();
}
public String getString(String aColumnName) {
String[] rowStr = (String[])list.elementAt(row);
return rowStr[getColumnIndex(aColumnName)];
// Vector row = (Vector)list.elementAt(FRow);
// return row.elementAt(getColumnIndex(aColumnName)).toString();
}
public void setSql(String tableName) {
String Str="";
Str="select * from "+tableName+" order by id desc";
strSql=Str;
}
public void setStrSql(String Str) {
strSql=Str;
}
public String getStrSql(){
return this.strSql;
}
public String getSql() {
return strSql;
}
public int getExecute() throws Exception{
if(excute()) return 1;
else return 0;
}
public boolean excute() throws Exception {
int id = 0;
try {
DBConnect dbc = new DBConnect(this.poolName,"CallableStatement");
CallableStatement cstmt = dbc.getConnection().prepareCall("{call P_viewPage(?,?,?,?,?,?,?,?,?,?,?)}",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
cstmt.setString(1,this.tableName);
cstmt.setString(2,this.fieldList);
cstmt.setString(3,this.primaryKey);
cstmt.setString(4,this.where);
cstmt.setString(5,this.order);
cstmt.setInt(6,this.sortType);
cstmt.setInt(7,super.getRecordCount());
cstmt.setInt(8,super.getPageSize());
cstmt.setInt(9,super.getPage());
cstmt.registerOutParameter(10,java.sql.Types.INTEGER);
cstmt.registerOutParameter(11,java.sql.Types.INTEGER);
//System.out.print(this.tableName+" "+this.fieldList+" "+this.primaryKey+" " +this.where+" "+this.order+" "+this.sortType+" "+super.getRecordCount()+" "+super.getPageSize()+" "+super.getPage());
cstmt.executeUpdate();
super.setRecordCount(cstmt.getInt(10));
super.setPageCount(cstmt.getInt(11));
//System.out.println(cstmt.getInt(10));
//System.out.println(cstmt.getInt(11));
ResultSet rs=cstmt.executeQuery();
//设置当前页
if(super.getPage()>=super.getPageCount()) super.setPage(super.getPageCount());
metaData = rs.getMetaData();
columnCount = metaData.getColumnCount();
columnNames = new String[columnCount];
// Get the column names and cache them.
// Then we can close the connection.
for(int column = 0; column < columnCount; column++) {
columnNames[column] = metaData.getColumnLabel(column+1);
}
//rs.last();
setShowPage(rs.getRow());
//rs.absolute(getRecode());
if(rs.next()){
for (int i=0;i<getPageSize();i++){
String[] rsRow = {};
rsRow = new String[columnCount];
for(int column = 0; column < columnCount; column++) {
rsRow[column] = rs.getString(column+1);
}
list.addElement(rsRow);
// list.addElement(rs.getObject(i));
if (!rs.next())
break;
}
}
rs.close();
cstmt.close();
dbc.close();
return true;
}
catch (SQLException sqle){
sqle.printStackTrace();
return false;
}
}
/**
* @return poolName
*/
public String getPoolName() {
return poolName;
}
/**
* @param poolName 要设置的 poolName
*/
public void setPoolName(String poolName) {
this.poolName = poolName;
}
}
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Types;
/**
* Title: 物品列表
* Description:
* Copyright: Copyright (c) 2007
* Company: USI
* @author: LJ
* @version 1.0
*/
public class Listss extends Pagess {
private String[] columnNames = {};
private ResultSetMetaData metaData;
int row=0;
int columnCount=0;
String strSql="";
int execute=0;
String sql="";
private String poolName;
private String tableName="";
private String fieldList="";
private String primaryKey="";
private String where="";
private String order="";
private int sortType=1;
/**
* @return fieldList
*/
public String getFieldList() {
return fieldList;
}
/**
* @param fieldList 要设置的 fieldList
*/
public void setFieldList(String fieldList) {
this.fieldList = fieldList;
}
/**
* @return order
*/
public String getOrder() {
return order;
}
/**
* @param order 要设置的 order
*/
public void setOrder(String order) {
this.order = order;
}
/**
* @return primaryKey
*/
public String getPrimaryKey() {
return primaryKey;
}
/**
* @param primaryKey 要设置的 primaryKey
*/
public void setPrimaryKey(String primaryKey) {
this.primaryKey = primaryKey;
}
/**
* @return sortType
*/
public int getSortType() {
return sortType;
}
/**
* @param sortType 要设置的 sortType
*/
public void setSortType(int sortType) {
this.sortType = sortType;
//System.out.println(this.sortType);
}
/**
* @return tableName
*/
public String getTableName() {
return tableName;
}
/**
* @param tableName 要设置的 tableName
*/
public void setTableName(String tableName) {
this.tableName = tableName;
}
/**
* @return where
*/
public String getWhere() {
return where;
}
/**
* @param where 要设置的 where
*/
public void setWhere(String where) {
this.where = where;
}
public Listss() throws Exception{
super();
}
public String getColumnName(int column) {
if (column>=columnCount)
column=columnCount-1;
if (column<0)
column=0;
if (columnNames[column] != null) {
return columnNames[column];
} else
return "";
}
public int getColumnIndex(String name) {
int ok=0;
if (name!=null) {
for (int i=0;i<columnCount;i++) {
String temp=columnNames[i].toLowerCase();
name=name.toLowerCase();
if (name.equals(temp))
{
ok= i;
break;
}
}
}
return ok;
}
public void setRow(int i) {
row=i;
}
public int getRow(){
return this.row;
}
public int getColumnCount() {
return columnNames.length;
}
public Class getColumnClass(int column) {
int type;
try {
type = metaData.getColumnType(column+1);
}
catch (SQLException e) {
return null;
}
switch(type) {
case Types.CHAR:
case Types.VARCHAR:
case Types.LONGVARCHAR:
return String.class;
case Types.BIT:
return Boolean.class;
case Types.TINYINT:
case Types.SMALLINT:
case Types.INTEGER:
return Integer.class;
case Types.BIGINT:
return Long.class;
case Types.FLOAT:
case Types.DOUBLE:
return Double.class;
case Types.DATE:
return java.sql.Date.class;
default:
return Object.class;
}
}
public String getString(int aColumn) {
String[] rowStr = (String[])list.elementAt(row);
return rowStr[aColumn];
// Vector row = (Vector)list.elementAt(FRow);
// return row.elementAt(aColumn).toString();
}
public String getString(String aColumnName) {
String[] rowStr = (String[])list.elementAt(row);
return rowStr[getColumnIndex(aColumnName)];
// Vector row = (Vector)list.elementAt(FRow);
// return row.elementAt(getColumnIndex(aColumnName)).toString();
}
public void setSql(String tableName) {
String Str="";
Str="select * from "+tableName+" order by id desc";
strSql=Str;
}
public void setStrSql(String Str) {
strSql=Str;
}
public String getStrSql(){
return this.strSql;
}
public String getSql() {
return strSql;
}
public int getExecute() throws Exception{
if(excute()) return 1;
else return 0;
}
public boolean excute() throws Exception {
int id = 0;
try {
DBConnect dbc = new DBConnect(this.poolName,"CallableStatement");
CallableStatement cstmt = dbc.getConnection().prepareCall("{call P_viewPage(?,?,?,?,?,?,?,?,?,?,?)}",ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
cstmt.setString(1,this.tableName);
cstmt.setString(2,this.fieldList);
cstmt.setString(3,this.primaryKey);
cstmt.setString(4,this.where);
cstmt.setString(5,this.order);
cstmt.setInt(6,this.sortType);
cstmt.setInt(7,super.getRecordCount());
cstmt.setInt(8,super.getPageSize());
cstmt.setInt(9,super.getPage());
cstmt.registerOutParameter(10,java.sql.Types.INTEGER);
cstmt.registerOutParameter(11,java.sql.Types.INTEGER);
//System.out.print(this.tableName+" "+this.fieldList+" "+this.primaryKey+" " +this.where+" "+this.order+" "+this.sortType+" "+super.getRecordCount()+" "+super.getPageSize()+" "+super.getPage());
cstmt.executeUpdate();
super.setRecordCount(cstmt.getInt(10));
super.setPageCount(cstmt.getInt(11));
//System.out.println(cstmt.getInt(10));
//System.out.println(cstmt.getInt(11));
ResultSet rs=cstmt.executeQuery();
//设置当前页
if(super.getPage()>=super.getPageCount()) super.setPage(super.getPageCount());
metaData = rs.getMetaData();
columnCount = metaData.getColumnCount();
columnNames = new String[columnCount];
// Get the column names and cache them.
// Then we can close the connection.
for(int column = 0; column < columnCount; column++) {
columnNames[column] = metaData.getColumnLabel(column+1);
}
//rs.last();
setShowPage(rs.getRow());
//rs.absolute(getRecode());
if(rs.next()){
for (int i=0;i<getPageSize();i++){
String[] rsRow = {};
rsRow = new String[columnCount];
for(int column = 0; column < columnCount; column++) {
rsRow[column] = rs.getString(column+1);
}
list.addElement(rsRow);
// list.addElement(rs.getObject(i));
if (!rs.next())
break;
}
}
rs.close();
cstmt.close();
dbc.close();
return true;
}
catch (SQLException sqle){
sqle.printStackTrace();
return false;
}
}
/**
* @return poolName
*/
public String getPoolName() {
return poolName;
}
/**
* @param poolName 要设置的 poolName
*/
public void setPoolName(String poolName) {
this.poolName = poolName;
}
}
4,条件查询页面recordsListSearch.jsp,主要是根据数据库字段来组成查询条件,值得注意的是:
<select size="1" name="D2" onChange="javascript:onChangeText();">
<option value="strPoNo">P/O</option>
<option value="strGrNo">收货GR NO.</option>
<option value="strMaterialNo">物料编号</option>
<option value="strMaterialName">物料名称</option>
<option value="strDate">日期</option>
<option value="nItem">Item</option>
<option value="nCount">数量</option>
<option value="strApplicant">请购人</option>
<option value="strDeptName">所属部门</option>
</select>
<option value="strPoNo">P/O</option>
<option value="strGrNo">收货GR NO.</option>
<option value="strMaterialNo">物料编号</option>
<option value="strMaterialName">物料名称</option>
<option value="strDate">日期</option>
<option value="nItem">Item</option>
<option value="nCount">数量</option>
<option value="strApplicant">请购人</option>
<option value="strDeptName">所属部门</option>
</select>
这里option的值都完全是数据表中的字段名,因为最终是靠这些字段来生成SQL语句中的Where查询条件。
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>查询</title>
</head>
<style type="text/css">
<!--
BODY {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
TD {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
-->
</style>
<script language="JavaScript" src="files/My97DatePicker/WdatePicker.js"></script>
<script language="javascript">
<!--
//日历与普通输入框切换
function onChangeText(){
if(form2.D2.value=="strDate"){
eval("document.getElementById('T2').style.display='block'");
eval("document.getElementById('T1').style.display='none'");
}else{
eval("document.getElementById('T2').style.display='none'");
eval("document.getElementById('T1').style.display='block'");
}
}
//根据条件组合生成SQL语句中的where条件
function query_item()
{
var str2="";
var str3="";
str2 += form2.D2.value + " ";
str2 += form2.D3.value + " ";
if (form2.D3.value=="like")
str2 += "'%" + form2.T1.value + "%'" + " ";
else {
if (form2.D2.value=="nItem"||form2.D2.value=="nCount")
str2 += "" + form2.T1.value + "" + " ";
else{
if(document.getElementById('T2').style.display=='block')
str2 += "'" + form2.T2.value + "'" + " ";
else
str2 += "'" + form2.T1.value + "'" + " "
}
}
str3 = form2.S2.value;
if (str3.search(str2)<0)
{
if (form2.S2.value!="")
str2 = form2.S2.value + form2.D1.value + " " + str2 + " ";
else
//str2 = "where" + " " + str2 + " "
str2 =" " + str2 + " ";
if(document.getElementById('T2').style.display=='block'){
if (form2.T2.value!="")
{
form2.S2.value = str2;
form2.T2.value = "";
}
}else{
if (form2.T1.value!="")
{
form2.S2.value = str2;
form2.T1.value = "";
}
}
}
}
//清空对象
function set_null()
{
form2.D1.options[0].selected
form2.D2.options[0].selected
form2.D3.options[0].selected
form2.T1.value = ""
form2.S2.value = ""
}
//-->
</script>
<body>
<table align="left" width="102%">
<tr>
<td width="24%" height="30" background="images/opbg.gif"><img src="images/a.gif">
当前位置:查询
</td>
</tr>
<tr>
<td style="height:60px"></td>
</tr>
<tr>
<td>
<form method="post" action="recordsListResult.jsp" name="form2" >
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" class="search">
<tbody>
<tr>
<td height="30" background="images/s_02.gif"><img src="images/s_01.gif" width="284" height="30"></td>
</tr>
<tr>
<td bgcolor="#f1f4f5" height="32">
<table>
<tr>
<td width="64">
<select size="1" name="D1">
<option value="and">并且</option>
<option value="or">或者</option>
</select>
</td>
<td width="39">
字段:
</td>
<td width="83">
<select size="1" name="D2" onChange="javascript:onChangeText();">
<option value="strPoNo">P/O</option>
<option value="strGrNo">收货GR NO.</option>
<option value="strMaterialNo">物料编号</option>
<option value="strMaterialName">物料名称</option>
<option value="strDate">日期</option>
<option value="nItem">Item</option>
<option value="nCount">数量</option>
<option value="strApplicant">请购人</option>
<option value="strDeptName">所属部门</option>
</select>
</td>
<td width="61">
查询条件:
</td>
<td width="77">
<select size="1" name="D3">
<option selected value="=">等于</option>
<option value=">">大于</option>
<option value="<">小于</option>
<option value="like">类似</option>
</select>
</td>
<td width="145">
<input type="text" name="T1" id="T1" size="20">
<input type="text" name="T2" id="T2" size="20" style="display:none" class="Wdate" onFocus="new WdatePicker(this)">
</td>
<td width="95">
<input type=button value="确定" name="B1"
<input type=button value="清除" name="B2"
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="38">
<textarea rows="3" name="S2" cols="80" style="font-family: 宋体; font-size: 9pt" readonly></textarea>
<input type="image" src="images/sear1.gif" name="sm1">
</td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>查询</title>
</head>
<style type="text/css">
<!--
BODY {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
TD {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
-->
</style>
<script language="JavaScript" src="files/My97DatePicker/WdatePicker.js"></script>
<script language="javascript">
<!--
//日历与普通输入框切换
function onChangeText(){
if(form2.D2.value=="strDate"){
eval("document.getElementById('T2').style.display='block'");
eval("document.getElementById('T1').style.display='none'");
}else{
eval("document.getElementById('T2').style.display='none'");
eval("document.getElementById('T1').style.display='block'");
}
}
//根据条件组合生成SQL语句中的where条件
function query_item()
{
var str2="";
var str3="";
str2 += form2.D2.value + " ";
str2 += form2.D3.value + " ";
if (form2.D3.value=="like")
str2 += "'%" + form2.T1.value + "%'" + " ";
else {
if (form2.D2.value=="nItem"||form2.D2.value=="nCount")
str2 += "" + form2.T1.value + "" + " ";
else{
if(document.getElementById('T2').style.display=='block')
str2 += "'" + form2.T2.value + "'" + " ";
else
str2 += "'" + form2.T1.value + "'" + " "
}
}
str3 = form2.S2.value;
if (str3.search(str2)<0)
{
if (form2.S2.value!="")
str2 = form2.S2.value + form2.D1.value + " " + str2 + " ";
else
//str2 = "where" + " " + str2 + " "
str2 =" " + str2 + " ";
if(document.getElementById('T2').style.display=='block'){
if (form2.T2.value!="")
{
form2.S2.value = str2;
form2.T2.value = "";
}
}else{
if (form2.T1.value!="")
{
form2.S2.value = str2;
form2.T1.value = "";
}
}
}
}
//清空对象
function set_null()
{
form2.D1.options[0].selected
form2.D2.options[0].selected
form2.D3.options[0].selected
form2.T1.value = ""
form2.S2.value = ""
}
//-->
</script>
<body>
<table align="left" width="102%">
<tr>
<td width="24%" height="30" background="images/opbg.gif"><img src="images/a.gif">
当前位置:查询
</td>
</tr>
<tr>
<td style="height:60px"></td>
</tr>
<tr>
<td>
<form method="post" action="recordsListResult.jsp" name="form2" >
<table width="600" border="1" align="center" cellpadding="0" cellspacing="0" class="search">
<tbody>
<tr>
<td height="30" background="images/s_02.gif"><img src="images/s_01.gif" width="284" height="30"></td>
</tr>
<tr>
<td bgcolor="#f1f4f5" height="32">
<table>
<tr>
<td width="64">
<select size="1" name="D1">
<option value="and">并且</option>
<option value="or">或者</option>
</select>
</td>
<td width="39">
字段:
</td>
<td width="83">
<select size="1" name="D2" onChange="javascript:onChangeText();">
<option value="strPoNo">P/O</option>
<option value="strGrNo">收货GR NO.</option>
<option value="strMaterialNo">物料编号</option>
<option value="strMaterialName">物料名称</option>
<option value="strDate">日期</option>
<option value="nItem">Item</option>
<option value="nCount">数量</option>
<option value="strApplicant">请购人</option>
<option value="strDeptName">所属部门</option>
</select>
</td>
<td width="61">
查询条件:
</td>
<td width="77">
<select size="1" name="D3">
<option selected value="=">等于</option>
<option value=">">大于</option>
<option value="<">小于</option>
<option value="like">类似</option>
</select>
</td>
<td width="145">
<input type="text" name="T1" id="T1" size="20">
<input type="text" name="T2" id="T2" size="20" style="display:none" class="Wdate" onFocus="new WdatePicker(this)">
</td>
<td width="95">
<input type=button value="确定" name="B1"
<input type=button value="清除" name="B2"
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td height="38">
<textarea rows="3" name="S2" cols="80" style="font-family: 宋体; font-size: 9pt" readonly></textarea>
<input type="image" src="images/sear1.gif" name="sm1">
</td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</table>
</body>
</html>
5,分页显示页面recordsListResult.jsp,完成分页,基本的参数也在这里配置以最终传入分页存储过程完成分页。
由于这个页面是我们配置的地方,所以这里详加说明:
比较重要的地方我会以
<!-----
------>
标出并加以详细说明;
要进行更改和配置的地方以
<!--******
******-->
标出,同时会在前面配以编号显示,读者朋友可以根据编号逐一查阅。
<!-------------------------------------
本页功能说明
该jsp模板是最简单的view实现的一个例子,主要根据上个页面查询的得到的where条件进行数据的分页显示
由于不含Form,实现起来较简单,开发者可以直接通过“引入”及“配置”来完成页面设计
需要更改的地方以******标记标出
--------------------------------------->
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!----------------------------------------
jsp:useBean id="list1" scope="request" class="sqlDBconnect.Listss" 的说明:
javaBean,后台用存储过程实现分页,该类完成分页存储过程参数的传递,且该类与应用无关!
------------------------------------------>
<jsp:useBean id="list1" scope="request" class="sqlDBconnect.Listss" />
<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>显示</title>
<html:base/>
<style type="text/css">
<!--
BODY {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
TD {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
-->
</style>
<link href="css/barStyle.css" rel="stylesheet" type="text/css">
<link href="css/search1.css" rel="stylesheet" type="text/css">
<link href="css/css1.css" rel="stylesheet" type="text/css">
<link href="css/ny.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#fffff7" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<script language="JavaScript">
//分页跳转
function Jumping(){
document.PageForm.submit();
return ;
}
</script>
<table align="left" width="102%">
<tr>
<td width="24%" height="30" background="images/opbg.gif"><img src="images/a.gif">
当前位置:查询结果
</td>
</tr>
<tr>
<td style="height:60px"></td>
</tr>
<tr>
<td>
<!--1,****************************************
c:set var="selfweb" value="recordsListResult" scope="request" 说明:
JSP中JSTL标签,生成request范围内的一个对象,主要为了指定devPage.jsp中的跳转页面,开发者应该根据不
同的显示页面为对象指定不同的值,一般是当前页名,注意不要带扩展名.
*******************************************-->
<c:set var="selfweb" value="recordsListResult" scope="request"/>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#b6bfdc" bgcolor="#b6bfdc" class="search">
<!--2,********************************************
td height="21" colspan="12" 说明:
首行应该显示的列数,根据需要确定要显示的列数,适时更改colspan的值
***********************************************-->
<tr bgcolor="#FFFFFF" >
<td height="21" colspan="12"><img src="images/sea.gif" width="354" height="21"></td>
</tr>
<tr class="bar1">
<!--3,*******************************************
要显示的列信息说明
这是数据表中应该显示出来给用户查看的列名及相关信息,根据需要显示,不必全部列出
*********************************************-->
<td width="8%" align="center" >P/O</td>
<td width="8%" align="center" >GR NO.</td>
<td width="8%" align="center" >物料编号</td>
<td width="9%" align="center" >物料名称</td>
<td width="9%" align="center" >日期</td>
<td width="6%" align="center">Item</td>
<td width="6%" align="center">数量</td>
<td width="7%" align="center">单价</td>
<td width="7%" align="center">请购人</td>
<td width="8%" align="center">所属部门</td>
<td width="8%" align="center">签收日期</td>
<td width="16%" align="center">物料描述</td>
</tr>
<!--4,************************
分页存储过程的一些参数
PageSize 规定每页应该显示多少条记录,默认20条,可以根据需要自行更改
ShowPage 当前显示第几页,初始化时默认显示第一页
PageCount 应该显示的页数,该值会根据前两个参数由系统自动更新
*************************-->
<c:set var="PageSize" scope="request" value="20"/>
<c:set var="ShowPage" scope="request" value="1"/>
<c:set var="PageCount" scope="request" value="0"/>
<%
//receive the parameter from the previousPage and nextPage
String Page=request.getParameter("page");
request.setAttribute("Page",Page);
%>
<logic:notEmpty name="Page">
<c:set var="ShowPage" scope="request">
<c:out value="${Page}"/>
</c:set>
</logic:notEmpty>
<!--set the property of List-->
<c:set target="${list1}" property="pageSize">
<c:out value="${PageSize}"/>
</c:set>
<c:set target="${list1}" property="page">
<c:out value="${ShowPage}"/>
</c:set>
<!--5,************************************
由查询页面传入的查询条件的说明
接收到的S2为上一个查询页面传入的查询条件,可以将此查询条件和其它信息进行组合生成最终的SQL
Where条件。
**************************************-->
<%
String S2=request.getParameter("S2") ;
if(S2!=null)
session.setAttribute("S2",S2);
%>
<!--6,**************************************
分页类Listss参数的说明
poolName 数据库连接池名,为proxool.xml配置的数据库连接池的别名
tableName 当前Jsp页面要显示的数据表名
fieldList 要用到数据表中的哪些字段,列出这些字段名,以逗号作为分隔符
primaryKey 数据表中以哪个字段作为主键
order 排序 不含'order by'字符,如nId desc,strPoNo asc,当SortType=3时生效
sortType 排序规则 1:正序asc 2:倒序desc 3:多列排序,如果取1或2则是对主键排序
******************************************-->
<c:set target="${list1}" property="poolName" value="bpDbCon"/>
<c:set target="${list1}" property="tableName" value="recordsList"/>
<c:set target="${list1}" property="fieldList" value="strPoNo,strGrNo,strDate,nItem,nCount,fltUnitPrice,strApplicant,strMaterialDesc,strMaterialNo,strMaterialName,strDeptName,strSignDate"/>
<c:set target="${list1}" property="primaryKey" value="nId"/>
<!--if the "S2" var is not null,set the strSql property of List-->
<logic:notEmpty name="S2">
<c:set target="${list1}" property="where">
<c:out escapeXml="false" value="${S2}"/>
</c:set>
</logic:notEmpty>
<c:set target="${list1}" property="order" value=""/>
<c:set target="${list1}" property="sortType" value="2"/>
<c:if test="${list1.execute==1}">
<c:set var="ShowPage" scope="request">
<c:out value="${list1.page}"/>
</c:set>
<c:set var="PageCount" scope="request">
<c:out value="${list1.pageCount}"/>
</c:set>
<c:set var="PageSize" scope="request">
<c:out value="${list1.pageSize}"/>
</c:set>
<c:forEach var="i" begin="0" end="${list1.pageSize-1}" step="1">
<c:set var="Class" value="search3"/>
<c:if test="${i%2==0}">
<c:set var="Class" value="search4"/>
</c:if>
<c:if test="${i<list1.listCount}">
<c:set target="${list1}" property="row">
<c:out value="${i}"/>
</c:set>
<!--7,****************************************
取出要显示的列值
不管是int,String,float,double等都可以list1.getString()来取值,传入参数为上面所列出的数据表中字段名
******************************************-->
<tr class='<c:out value="${Class}"/>' onmouseover="this.style.backgroundColor='#dae2ed'" onmouseout="this.style.backgroundColor=''">
<td align="center"><%=list1.getString("strPoNo")%></td>
<td align="center"><%=list1.getString("strGrNo")%></td>
<td align="center"><%=list1.getString("strMaterialNo")%></td>
<td align="center"><%=list1.getString("strMaterialName")%></td>
<td align="center"><%=list1.getString("strDate")%></td>
<td align="center"><%=list1.getString("nItem")%></td>
<td align="center"><%=list1.getString("nCount")%></td>
<td align="center"><%=list1.getString("fltUnitPrice")%></td>
<td align="center"><%=list1.getString("strApplicant")%></td>
<td align="center"><%=list1.getString("strDeptName")%></td>
<td align="center"><%=list1.getString("strSignDate")%></td>
<td align="center"><%=list1.getString("strMaterialDesc")%></td>
</tr>
</c:if>
</c:forEach>
</c:if>
<tr onmouseover="this.style.backgroundColor='#dae2ed'" onmouseout="this.style.backgroundColor=''">
<td colspan="12">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<!-------------------------------------
引入分页脚本
--------------------------------------->
<%@ include file="devPage.jsp"%>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html:html>
本页功能说明
该jsp模板是最简单的view实现的一个例子,主要根据上个页面查询的得到的where条件进行数据的分页显示
由于不含Form,实现起来较简单,开发者可以直接通过“引入”及“配置”来完成页面设计
需要更改的地方以******标记标出
--------------------------------------->
<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!----------------------------------------
jsp:useBean id="list1" scope="request" class="sqlDBconnect.Listss" 的说明:
javaBean,后台用存储过程实现分页,该类完成分页存储过程参数的传递,且该类与应用无关!
------------------------------------------>
<jsp:useBean id="list1" scope="request" class="sqlDBconnect.Listss" />
<html:html lang="true">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>显示</title>
<html:base/>
<style type="text/css">
<!--
BODY {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
TD {
FONT-SIZE: 12px; FONT-FAMILY: "宋体"
}
-->
</style>
<link href="css/barStyle.css" rel="stylesheet" type="text/css">
<link href="css/search1.css" rel="stylesheet" type="text/css">
<link href="css/css1.css" rel="stylesheet" type="text/css">
<link href="css/ny.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#fffff7" topmargin="0" bottommargin="0" leftmargin="0" rightmargin="0">
<script language="JavaScript">
//分页跳转
function Jumping(){
document.PageForm.submit();
return ;
}
</script>
<table align="left" width="102%">
<tr>
<td width="24%" height="30" background="images/opbg.gif"><img src="images/a.gif">
当前位置:查询结果
</td>
</tr>
<tr>
<td style="height:60px"></td>
</tr>
<tr>
<td>
<!--1,****************************************
c:set var="selfweb" value="recordsListResult" scope="request" 说明:
JSP中JSTL标签,生成request范围内的一个对象,主要为了指定devPage.jsp中的跳转页面,开发者应该根据不
同的显示页面为对象指定不同的值,一般是当前页名,注意不要带扩展名.
*******************************************-->
<c:set var="selfweb" value="recordsListResult" scope="request"/>
<table width="780" border="0" align="center" cellpadding="0" cellspacing="1" bordercolor="#b6bfdc" bgcolor="#b6bfdc" class="search">
<!--2,********************************************
td height="21" colspan="12" 说明:
首行应该显示的列数,根据需要确定要显示的列数,适时更改colspan的值
***********************************************-->
<tr bgcolor="#FFFFFF" >
<td height="21" colspan="12"><img src="images/sea.gif" width="354" height="21"></td>
</tr>
<tr class="bar1">
<!--3,*******************************************
要显示的列信息说明
这是数据表中应该显示出来给用户查看的列名及相关信息,根据需要显示,不必全部列出
*********************************************-->
<td width="8%" align="center" >P/O</td>
<td width="8%" align="center" >GR NO.</td>
<td width="8%" align="center" >物料编号</td>
<td width="9%" align="center" >物料名称</td>
<td width="9%" align="center" >日期</td>
<td width="6%" align="center">Item</td>
<td width="6%" align="center">数量</td>
<td width="7%" align="center">单价</td>
<td width="7%" align="center">请购人</td>
<td width="8%" align="center">所属部门</td>
<td width="8%" align="center">签收日期</td>
<td width="16%" align="center">物料描述</td>
</tr>
<!--4,************************
分页存储过程的一些参数
PageSize 规定每页应该显示多少条记录,默认20条,可以根据需要自行更改
ShowPage 当前显示第几页,初始化时默认显示第一页
PageCount 应该显示的页数,该值会根据前两个参数由系统自动更新
*************************-->
<c:set var="PageSize" scope="request" value="20"/>
<c:set var="ShowPage" scope="request" value="1"/>
<c:set var="PageCount" scope="request" value="0"/>
<%
//receive the parameter from the previousPage and nextPage
String Page=request.getParameter("page");
request.setAttribute("Page",Page);
%>
<logic:notEmpty name="Page">
<c:set var="ShowPage" scope="request">
<c:out value="${Page}"/>
</c:set>
</logic:notEmpty>
<!--set the property of List-->
<c:set target="${list1}" property="pageSize">
<c:out value="${PageSize}"/>
</c:set>
<c:set target="${list1}" property="page">
<c:out value="${ShowPage}"/>
</c:set>
<!--5,************************************
由查询页面传入的查询条件的说明
接收到的S2为上一个查询页面传入的查询条件,可以将此查询条件和其它信息进行组合生成最终的SQL
Where条件。
**************************************-->
<%
String S2=request.getParameter("S2") ;
if(S2!=null)
session.setAttribute("S2",S2);
%>
<!--6,**************************************
分页类Listss参数的说明
poolName 数据库连接池名,为proxool.xml配置的数据库连接池的别名
tableName 当前Jsp页面要显示的数据表名
fieldList 要用到数据表中的哪些字段,列出这些字段名,以逗号作为分隔符
primaryKey 数据表中以哪个字段作为主键
order 排序 不含'order by'字符,如nId desc,strPoNo asc,当SortType=3时生效
sortType 排序规则 1:正序asc 2:倒序desc 3:多列排序,如果取1或2则是对主键排序
******************************************-->
<c:set target="${list1}" property="poolName" value="bpDbCon"/>
<c:set target="${list1}" property="tableName" value="recordsList"/>
<c:set target="${list1}" property="fieldList" value="strPoNo,strGrNo,strDate,nItem,nCount,fltUnitPrice,strApplicant,strMaterialDesc,strMaterialNo,strMaterialName,strDeptName,strSignDate"/>
<c:set target="${list1}" property="primaryKey" value="nId"/>
<!--if the "S2" var is not null,set the strSql property of List-->
<logic:notEmpty name="S2">
<c:set target="${list1}" property="where">
<c:out escapeXml="false" value="${S2}"/>
</c:set>
</logic:notEmpty>
<c:set target="${list1}" property="order" value=""/>
<c:set target="${list1}" property="sortType" value="2"/>
<c:if test="${list1.execute==1}">
<c:set var="ShowPage" scope="request">
<c:out value="${list1.page}"/>
</c:set>
<c:set var="PageCount" scope="request">
<c:out value="${list1.pageCount}"/>
</c:set>
<c:set var="PageSize" scope="request">
<c:out value="${list1.pageSize}"/>
</c:set>
<c:forEach var="i" begin="0" end="${list1.pageSize-1}" step="1">
<c:set var="Class" value="search3"/>
<c:if test="${i%2==0}">
<c:set var="Class" value="search4"/>
</c:if>
<c:if test="${i<list1.listCount}">
<c:set target="${list1}" property="row">
<c:out value="${i}"/>
</c:set>
<!--7,****************************************
取出要显示的列值
不管是int,String,float,double等都可以list1.getString()来取值,传入参数为上面所列出的数据表中字段名
******************************************-->
<tr class='<c:out value="${Class}"/>' onmouseover="this.style.backgroundColor='#dae2ed'" onmouseout="this.style.backgroundColor=''">
<td align="center"><%=list1.getString("strPoNo")%></td>
<td align="center"><%=list1.getString("strGrNo")%></td>
<td align="center"><%=list1.getString("strMaterialNo")%></td>
<td align="center"><%=list1.getString("strMaterialName")%></td>
<td align="center"><%=list1.getString("strDate")%></td>
<td align="center"><%=list1.getString("nItem")%></td>
<td align="center"><%=list1.getString("nCount")%></td>
<td align="center"><%=list1.getString("fltUnitPrice")%></td>
<td align="center"><%=list1.getString("strApplicant")%></td>
<td align="center"><%=list1.getString("strDeptName")%></td>
<td align="center"><%=list1.getString("strSignDate")%></td>
<td align="center"><%=list1.getString("strMaterialDesc")%></td>
</tr>
</c:if>
</c:forEach>
</c:if>
<tr onmouseover="this.style.backgroundColor='#dae2ed'" onmouseout="this.style.backgroundColor=''">
<td colspan="12">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<!-------------------------------------
引入分页脚本
--------------------------------------->
<%@ include file="devPage.jsp"%>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html:html>
6,分页脚本devPage.jsp,提取的共用代码段,完成翻页
<%@page contentType="text/html; charset=gb2312" language="java" errorPage=""%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<form method="post" action='<c:out value="${selfweb}"/>.jsp' name="PageForm">
<tr>
<td class="search4" width="100%" height="100%">
<img src="images/y1.gif" width="5" height="7">
<a href='<c:out value="${selfweb}"/>.jsp?' target="_self">第一页</a>
<c:if test="${ShowPage==1}">
<img src="images/y2.gif" width="4" height="7">前一页
</c:if>
<c:if test="${ShowPage!=1}">
<img src="images/y2.gif" width="4" height="7">
<%request.setAttribute("prev",String.valueOf(Integer.parseInt(request.getAttribute("ShowPage").toString())-1));%>
<a href='<c:out value="${selfweb}"/>.jsp?page=<c:out value="${prev}"/>' target="_self">前一页</a>
</c:if>
<c:if test="${ShowPage==PageCount}"> <img src="images/y3.gif" width="4" height="7">下一页
</c:if>
<c:if test="${ShowPage!=PageCount}"> <img src="images/y3.gif" width="4" height="7">
<%request.setAttribute("next",String.valueOf(Integer.parseInt(request.getAttribute("ShowPage").toString())+1));%>
<a href='<c:out value="${selfweb}"/>.jsp?page=<c:out value="${next}"/>' target="_self">下一页</a>
</c:if>
<img src="images/y4.gif" width="5" height="7"><a href='<c:out value="${selfweb}"/>.jsp?page=<c:out value="${list1.pageCount}"/>' target="_self">最后一页</a>
记录总数<c:out value="${list1.recordCount}"/>
每页<c:out value="${PageSize}"/>
当前页[<c:out value="${ShowPage}"/>/<c:out value="${list1.pageCount}"/>页]
页数
<select name="page" onchange="Jumping()" style="width:50px">
<c:if test="${ShowPage!=0}">
<option value='<c:out value="${ShowPage}"/>'><c:out value="${ShowPage}"/></option>
</c:if>
<c:if test="${list1.pageCount>=1}">
<c:forEach var="i" begin="1" end="${list1.pageCount}" step="1">
<option value='<c:out value="${i}"/>'><c:out value="${i}"/></option>
</c:forEach>
</c:if>
</select>
</td>
</tr>
</form>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>
<form method="post" action='<c:out value="${selfweb}"/>.jsp' name="PageForm">
<tr>
<td class="search4" width="100%" height="100%">
<img src="images/y1.gif" width="5" height="7">
<a href='<c:out value="${selfweb}"/>.jsp?' target="_self">第一页</a>
<c:if test="${ShowPage==1}">
<img src="images/y2.gif" width="4" height="7">前一页
</c:if>
<c:if test="${ShowPage!=1}">
<img src="images/y2.gif" width="4" height="7">
<%request.setAttribute("prev",String.valueOf(Integer.parseInt(request.getAttribute("ShowPage").toString())-1));%>
<a href='<c:out value="${selfweb}"/>.jsp?page=<c:out value="${prev}"/>' target="_self">前一页</a>
</c:if>
<c:if test="${ShowPage==PageCount}"> <img src="images/y3.gif" width="4" height="7">下一页
</c:if>
<c:if test="${ShowPage!=PageCount}"> <img src="images/y3.gif" width="4" height="7">
<%request.setAttribute("next",String.valueOf(Integer.parseInt(request.getAttribute("ShowPage").toString())+1));%>
<a href='<c:out value="${selfweb}"/>.jsp?page=<c:out value="${next}"/>' target="_self">下一页</a>
</c:if>
<img src="images/y4.gif" width="5" height="7"><a href='<c:out value="${selfweb}"/>.jsp?page=<c:out value="${list1.pageCount}"/>' target="_self">最后一页</a>
记录总数<c:out value="${list1.recordCount}"/>
每页<c:out value="${PageSize}"/>
当前页[<c:out value="${ShowPage}"/>/<c:out value="${list1.pageCount}"/>页]
页数
<select name="page" onchange="Jumping()" style="width:50px">
<c:if test="${ShowPage!=0}">
<option value='<c:out value="${ShowPage}"/>'><c:out value="${ShowPage}"/></option>
</c:if>
<c:if test="${list1.pageCount>=1}">
<c:forEach var="i" begin="1" end="${list1.pageCount}" step="1">
<option value='<c:out value="${i}"/>'><c:out value="${i}"/></option>
</c:forEach>
</c:if>
</select>
</td>
</tr>
</form>
大功告成,不过你不要以为前面讲得那么多就配起来很繁琐,其它不然,前面所讲的只是让你对这种分页架构有一个大体的了解,从细节方面你只需要注意以下几个方面就可以:
1,配置Tomcat服务器(你别告诉我你不会,如果真不会,那没办法了你自行了断吧,嘿嘿)
2,建立数据库和结构表,对了还有分页存储过程P_viewPage.sql一并弄好
3,配置数据库连接池,几句话而已,你完全可以照我的例子弄
4,配查询页
一般情况下你只需更改下面这个地方:
<select size="1" name="D2" onChange="javascript:onChangeText();">
<option value="strPoNo">P/O</option>
<option value="strGrNo">收货GR NO.</option>
<option value="strMaterialNo">物料编号</option>
<option value="strMaterialName">物料名称</option>
<option value="strDate">日期</option>
<option value="nItem">Item</option>
<option value="nCount">数量</option>
<option value="strApplicant">请购人</option>
<option value="strDeptName">所属部门</option>
</select>
<option value="strPoNo">P/O</option>
<option value="strGrNo">收货GR NO.</option>
<option value="strMaterialNo">物料编号</option>
<option value="strMaterialName">物料名称</option>
<option value="strDate">日期</option>
<option value="nItem">Item</option>
<option value="nCount">数量</option>
<option value="strApplicant">请购人</option>
<option value="strDeptName">所属部门</option>
</select>
根据自己的数据表显示不同字段
5,配置分页显示
根据你自己的情况你最多只需更改上面提到的recordsListResult.jsp中的7个地方
Job Done!为你自己喝彩吧!
本文出自 “君临天下” 博客,请务必保留此出处http://wenlujun.blog.51cto.com/336737/68224
本文出自 51CTO.COM技术博客附件下载:
项目源码
项目源码