使用指南
1:该组件为jsp页面数据分页通用组件,可用于所有struts或jsp+javabean的页面分页功能
2:该组件使用非常简单,只需设定好各项属性,传入数据Vctor或者Sql语句,直接调用多态的
getContentHtml方法即可得到分好页的表格的html代码(string),在页面中<%=string%>即可
,之后的各种翻页操作业已封装在该html代码中,用户不必再操心。
3:该组件设置步骤
a.初始化分页组件实例
eg:OperatePage operatePage = new OperatePage();
b.设定分页表格数据所属的表单名
如不设置该属性,组件将自定义一个名为"form1" 的表单名来存放分页表格。
eg:operatePage.setFormName("formName");
c.设定翻页操作时的跳转页面
该属性必需设置,否则会报错。组件支持翻页时跳转到jsp页面(一般都是原jsp页面)
或者某一Action(struts架构)中。
eg:operatePage.setPageUrl("/jsp/managerAction.do");
eg:operatePage.setPageUrl("/jsp/testPage.jsp");
d.设定跳转类型
组件支持jsp和Action两种跳转类型,两种类型并无大的区别,只是设成Action时
将在翻页操作时传递一个"flag=OperatePage"的参数以便在用户Action中区分是翻页
操作。
operatePage.setForwardType(OperateConst.FOWARD_TYPE_ACTION);或者
operatePage.setForwardType(OperateConst.FOWARD_TYPE_JSP);
e.设定数据Vctor(如果传sql的话在此步骤设置数据库连接的Connection)
operatePage.setCon(con); 或者
operatePage.setDataVector(vecData);
f.设定是否需要单选或者多选列(可不设置,默认为true)
opeatePage.operatePage.setHasSelectItem(true);
g.设定单选或者多选列类型,元素名及对应的该行数据的主键值(如f步骤中设置为false这步省略)
//支持checkBox和radioButton两种
operatePage.setSelectType(OperateConst.SELECT_TYPE_RADIOBUTTON);
//可不设,默认为selectItem1
operatePage.setSelectItemName("selectItemName2");
//序号从1开始,可不设,默认为该行数据的第一列的值
operatePage.setSelectKeyArray( new int []{1,2,3});
h.设定数据标题列(如e步骤中设置为传sql此步省略)
operatePage.setContentTitle(new String[]{"col1","col2","col3","col4"});
g.前面的设置完成后,直接调以下API即可
/**
* @传Vector方式
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param request SevletRequest
* @param response SevletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize,int pm_iPageIndex,HttpServletRequest request, HttpServletResponse response) throws OperateException;
/**
* @传sql方式
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_sPageUrl jsp页面路径
* @param pm_sSql 查询的sql
* @param request SevletRequest
* @param response SevletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize,int pm_iPageIndex,String pm_sSql,HttpServletRequest request, HttpServletResponse response) throws OperateException;
/**
* @传sql方式
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_sPageUrl jsp页面路径
* @param pm_sSql 查询的sql
* @param pm_sDBType 数据库类型,如数据库直接支持数据库端分页,可提高效率
* @param request ServletRequest
* @param response ServletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize,int pm_iPageIndex,String pm_sSql,String pm_sDBType,HttpServletRequest request, HttpServletResponse response) throws OperateException;
4:实例
上述可能有点抽象,让我们来看两个完整使用实例:
第一种是直接在jsp页面中分页,即翻页操作时跳回原jsp页面中
我们来看看testPage.jsp的源代码:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<jsp:useBean id="operatePage" class="Action.OperatePage"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title></title>
</head>
<body>
<%
/**********这是获取数据vector的代码***********/
DbSelect a = new DbSelect();
Vector b = a.getManagerContentByMain_Key(1);
/*form2是你的jsp页面中的表单名*/
operatePage.setFormName("form2");
operatePage.setPageUrl("/jsp/testPage.jsp");
operatePage.setForwardType(OperateConst.FOWARD_TYPE_JSP);
operatePage.setDataVector(b);
operatePage.setHasSelectItem(true);
operatePage.setSelectType(OperateConst.SELECT_TYPE_CHECKBOX);
/*selectItemName是单选列的元素名,这样你可以用request.getParamer("selectItemName")来获取选中的值 ,如不设,默认为名字为selectItemName1*/
operatePage.setSelectItemName("selectItemName");
/*1,2,3表示你用request.getParamer("selectItemName")获取选中行的value为该行第一列,第二列,第三列的值的集合,各列的值以;分隔*/
operatePage.setSelectKeyArray( new int []{1,2,3});
operatePage.setContentTitle(new String[]{"col1","col2","col3","col4"});
/****15是初始的页大小,0是初始页码,之后翻页跳转时这两个值会被request中的新值自动更新*/
String c = operatePage.getContentHtml(15,0,request,response);
%>
<%=c %>
</script>
</body>
</html>
这是传Vctor方式,如果我们改成传sql的方式,只需做修改如下:
<%
/**********这是获取数据库连接的代码***********/
DbConnect a = new DbConnect();
Connection con = a.getOracleConnection();
String sql = "select * from safe_control_object";
operatePage.setFormName("form2");
operatePage.setPageUrl("/jsp/testPage.jsp");
operatePage.setForwardType(OperateConst.FOWARD_TYPE_JSP);
operatePage.setCon(con);
operatePage.setHasSelectItem(true);
operatePage.setSelectType(OperateConst.SELECT_TYPE_CHECKBOX);
operatePage.setSelectItemName("selectItemName2");
operatePage.setSelectKeyArray( new int []{1,2,3});
/* OperateConst.DBTYPE_ORACLE是数据库类型,该字段可传可不传,传的好处在于如果该类型数据库支持后端分页,则组件会采用后端分页的方式,效率较前端分页要高*/
String c = operatePage.getContentHtml(15,0,sql,OperateConst.DBTYPE_ORACLE,request,response);
%>
我们再来看看跳转到Action中的情况,这是翻页Action的源码示例
public class ManagerAction extends DispatchAction{
OperatePage operatePage = new OperatePage();
/**********这是获取数据vector的代码***********/
DbSelect a = new DbSelect();
Vector b = a.getManagerContentByMain_Key(1);
public ActionForward pageOperate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
String c = operatePage.getContentHtml(15,0,request,response);
HttpSession session = request.getSession();
session.setAttribute("testPage",c);
return mapping.findForward("testPage");
}
/**初始化的操作**/
public ActionForward initPageOperate(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
operatePage.setFormName("form2");
operatePage.setPageUrl("/jsp/managerAction.do");
operatePage.setForwardType(OperateConst.FOWARD_TYPE_ACTION);
operatePage.setDataVector(b);
operatePage.setHasSelectItem(true);
operatePage.setSelectType(OperateConst.SELECT_TYPE_RADIOBUTTON);
operatePage.setSelectItemName("selectItemName2");
operatePage.setSelectKeyArray( new int []{1,2,3});
operatePage.setContentTitle(new String[]{"col1","col2","col3","col4"});
String c = operatePage.getContentHtml(15,0,request,response);
/*把获得的分页后的html代码放到session中去*/
HttpSession session = request.getSession();
session.setAttribute("testPage",c);
/*testPage是你在struts-config.xml中配置的jsp页面的跳转forward**/
return mapping.findForward("testPage");
}
}
jps示例页面如下:
<%@ page language="java" contentType="text/html; charset=GB18030" pageEncoding="GB18030"%>
<jsp:useBean id="operatePage" class="Action.OperatePage"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title></title>
</head>
<body>
<%
String c = (String)session.getAttribute("testPage");
%>
<%=c %>
//flag=initPageOperate对应Action中初始化的操作
<a href="/jsp/managerAction.do?flag=initPageOperate">测试Action端分页</a>
这是传Vctor的方式,传sql和之前跳转到jsp中的类似,这里就不重复了
OK!这就是最常见的传sql和传Vector,及在原jsp页面跳转或者在Action中跳转的数据库分页组件操作,每种不过十几行代码即可搞定!
5:如果有什么不清楚的地方,请发email:qingyuan18@126.com,或者msn:tangqingyuan@chinamobilesz.com,
有做的不好的地方,请不吝赐教!
IOperatePage.java 文件
package Action;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.Vector;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface IOperatePage {
/**
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param request SevletRequest
* @param response SevletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize,int pm_iPageIndex,HttpServletRequest request, HttpServletResponse response) throws OperateException;
/**
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_sPageUrl jsp页面路径
* @param pm_sSql 查询的sql
* @param request SevletRequest
* @param response SevletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize,int pm_iPageIndex,String pm_sSql,HttpServletRequest request, HttpServletResponse response) throws OperateException;
/**
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_sPageUrl jsp页面路径
* @param pm_sSql 查询的sql
* @param pm_sDBType 数据库类型,如数据库直接支持数据库端分页,可提高效率
* @param request ServletRequest
* @param response ServletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize,int pm_iPageIndex,String pm_sSql,String pm_sDBType,HttpServletRequest request, HttpServletResponse response) throws OperateException;
}
OperateConst.java 文件
package Action;
public final class OperateConst {
/*
* 数据库类型
*/
public static final String DBTYPE_SQLSERVER = "sqlServer";
public static final String DBTYPE_DB2 = "DB2";
public static final String DBTYPE_ORACLE = "oracle";
/*
* 行数据选择类型
* 1:CheckBox
* 2:RadioButton
*/
public static final int SELECT_TYPE_CHECKBOX = 1;
public static final int SELECT_TYPE_RADIOBUTTON = 2;
/*
* 翻页跳转类型
* 1:跳回原jsp页面
* 2:跳回Action.do(struts框架)
*/
public static final int FOWARD_TYPE_JSP = 1;
public static final int FOWARD_TYPE_ACTION =2;
/*
*错误类型
*1:未设置url
*2:未设置标题
*3:Connection为空(传sql方式)
*4:未识别的数据库
*5:数据vector为空(非传sql方式)
*6:查询sql为空(传sql方式)
*/
public static final int EXCEPTION_TYPE_URL_IS_NULL = 1;
public static final int EXCEPTION_TYPE_TITLE_IS_NULL = 2;
public static final int EXCEPTION_TYPE_CON_IS_NULL = 3;
public static final int EXCEPTION_TYPE_DB_NOT_RECOGNIZE = 4;
public static final int EXCEPTION_TYPE_DATA_VECTOR_IS_NULL = 5;
public static final int EXCEPTION_TYPE_SQL_IS_NULL = 6;
}
OperateException.java 文件
package Action;
public class OperateException extends Exception {
private String ExceptionMsg= "OperatePage Exception:";
/**
* @param pm_iExceptionType 异常类型
* 根据定义的异常类型构造异常信息
*/
public OperateException(int pm_iExceptionType)
{
if(pm_iExceptionType == OperateConst.EXCEPTION_TYPE_CON_IS_NULL)
{
this.ExceptionMsg += "The Connection is null ! ";
}
else if(pm_iExceptionType == OperateConst.EXCEPTION_TYPE_DB_NOT_RECOGNIZE)
{
this.ExceptionMsg += "unrecoginze DBType!";
}
else if(pm_iExceptionType == OperateConst.EXCEPTION_TYPE_TITLE_IS_NULL)
{
this.ExceptionMsg += "The title is null";
}
else if(pm_iExceptionType == OperateConst.EXCEPTION_TYPE_URL_IS_NULL)
{
this.ExceptionMsg += "The pageUrl is null!";
}
else if(pm_iExceptionType == OperateConst.EXCEPTION_TYPE_DATA_VECTOR_IS_NULL)
{
this.ExceptionMsg += "The Data Vector is null!";
}
}
/**
* 打印异常信息
*/
public void printMsg()
{
System.out.println(this.ExceptionMsg);
}
}
OperatePage.java 文件
package Action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
import java.util.Vector;
import java.util.regex.Pattern;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.Tools;
public class OperatePage implements IOperatePage{
//表单名,如未设置,将把分页数据装在名为"form1"的表单中
private String formName = "";
//分页时跳转的页面url
private String pageUrl = "";
//数据容器,传sql时不必设
private Vector dataVector;
//数据连接,传sql时必需设置
private Connection con;
//分页表格标题头
private String[] contentTitle;
//行选择时的元素名,在设定选择模式时有效,默认为checkboxItem
private String selectItemName = "checkboxItem";
//行选择类型,单选和多选两种,在设定选择模式时有效,默认为多选
private int selectType = OperateConst.SELECT_TYPE_CHECKBOX;
//翻页跳转类型,到jsp和到Action两种(默认到jsp)
private int forwardType = OperateConst.FOWARD_TYPE_JSP;
//行选择时每行数据的值,与数据列对应,各列数据以;分隔(默认第一列)
private int [] selectKeyArray = {1};
//是否为选择模式,如为true,分页表格头列将加单选或多选按钮(默认为true)
private boolean hasSelectItem = true;
//用户是否未设置formName,组件自我控制逻辑,用户不必关心该属性
private boolean isSelfForm ;
/**
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_vecContent 数据内容
* @param request SevletRequest
* @param response SevletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize, int pm_iPageIndex, HttpServletRequest request, HttpServletResponse response) throws OperateException {
StringBuffer strBuf = new StringBuffer("");
int pageSize=pm_iPageSize;
int pageIndex=pm_iPageIndex;
String pageUrlReal = this.pageUrl;
//检查必要设置
if(this.contentTitle == null) /*如未设置标题头,抛出异常*/
{
throw new OperateException(OperateConst.EXCEPTION_TYPE_TITLE_IS_NULL);
}
if(this.pageUrl == null) /*如传递的跳转页面为空,抛出异常*/
{
throw new OperateException(OperateConst.EXCEPTION_TYPE_URL_IS_NULL);
}
if(this.dataVector == null) /*如未设置待分页数据,抛出异常*/
{
throw new OperateException(OperateConst.EXCEPTION_TYPE_DATA_VECTOR_IS_NULL);
}
// 判断是第一次进入还是翻页操作,设置相应的pageSize和pageIndex
if(request.getParameter("pageSizeText")==null)
{
pageSize = pm_iPageSize;
}
else if(!request.getParameter("pageSizeText").equals(""))
{
pageSize = Integer.parseInt(request.getParameter("pageSizeText"));
}
if(request.getParameter("pageIndex")==null)
{
pageIndex = pm_iPageIndex;
}
else if(request.getParameter("pageIndex")!=null)
{
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
}
//如跳转类型为Action,在url中加flag=pageOperate参数以便在用户的Action中标识跳转操作(默认为跳转到原jsp)
if(this.forwardType == OperateConst.FOWARD_TYPE_ACTION)
{
pageUrlReal = this.pageUrl+"?flag=pageOperate&";
}
else if(this.forwardType == OperateConst.FOWARD_TYPE_JSP)
{
pageUrlReal = this.pageUrl+"?";
}
//如未设置form名,则将分页数据封装在名为"form1"的表单中
if(this.getFormName().equals(""))
{
this.setFormName("form1");
this.setSelfForm(true);
strBuf.append("<form name="+this.formName+" action='' method=post>");
strBuf.append("\n");
}
else if( (!this.getFormName().equals(""))&&this.isSelfForm==true)
{
this.setFormName("form1");
strBuf.append("<form name="+this.formName+" action='' method=post>");
strBuf.append("\n");
}
//主体操作
Vector vecContent = this.dataVector;
strBuf.append("<table width=100% cellspacing=1 cellpadding=0 background=blue>");
//表格标题栏
strBuf.append("<tr >");
if(this.hasSelectItem)
{
strBuf.append("<td >");
if(this.selectType == OperateConst.SELECT_TYPE_CHECKBOX)
{
strBuf.append("<input type='checkbox' name='checkAll' onclick='doSelectAll();'/>");
}
strBuf.append("</td>");
}
for(int i=0;i<contentTitle.length;i++)
{
strBuf.append("<td>"+contentTitle[i]+"</td>");
}
strBuf.append("</tr>");
//表内容
//分页操作
int totalCount = vecContent.size();
int totalPage = totalCount/pageSize;
if(totalPage*pageSize<totalCount)
{
totalPage = totalPage + 1;
}
int beginIndex = pageIndex*pageSize ;
int endIndex = beginIndex + pageSize - 1;
if(endIndex >= totalCount)
{
endIndex = totalCount -1;
}
for(int i=beginIndex;i<=endIndex;i++)
{
Vector tmp = (Vector)vecContent.get(i);
strBuf.append("<tr>");
if(this.hasSelectItem) /*如设置行选择,增加selectItem列*/
{
strBuf.append("<td>");
if(this.selectType == OperateConst.SELECT_TYPE_CHECKBOX)
{
strBuf.append("<input type='checkbox' name='"+this.getSelectItemName()+"' value='");
for(int k=0;k<this.selectKeyArray.length;k++)
{
strBuf.append(tmp.get(selectKeyArray[k]-1)+";");
}
strBuf.append("'/>");
}
else if(this.selectType == OperateConst.SELECT_TYPE_RADIOBUTTON)
{
strBuf.append("<input type='radio' name='"+this.getSelectItemName()+"' value='");
for(int k=0;k<this.selectKeyArray.length;k++)
{
strBuf.append(tmp.get(selectKeyArray[k]-1)+";");
}
strBuf.append("'/>");
}
strBuf.append("</td>");
}
for(int j=0;j<tmp.size();j++)
{
strBuf.append("<td>"+tmp.get(j)+"</td>");
}
strBuf.append("</tr>");
}
strBuf.append("</table>");
strBuf.append("<br>");
if(pageIndex>0)
{
strBuf.append("|<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex=0' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>首页</font>");
strBuf.append("</a>|");
strBuf.append("<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex="+String.valueOf(pageIndex-1)+"' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>上一页</font>");
strBuf.append("</a>|");
}
if(pageIndex<totalPage-1)
{
strBuf.append("<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex="+String.valueOf(pageIndex+1)+"' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>下一页</font>");
strBuf.append("</a>|");
strBuf.append("<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex="+String.valueOf(totalPage-1)+"' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>末页</font>");
strBuf.append("</a>|");
}
strBuf.append("<font color='red'>每页显示</font>");
strBuf.append("<input type=text name=pageSizeText size=10 value="+pageSize+">");
strBuf.append("</input>");
strBuf.append("<font color='red'>行</font>");
//js脚本
strBuf.append("\n");
strBuf.append("<script language='JavaScript'>\n");
//提交翻页操作脚本
strBuf.append("function commonSubmit(formObject,url)"+"\n");
strBuf.append("{\n");
strBuf.append("formObject.action=url"+"\n");
//strBuf.append("alert(formObject.action)\n");
strBuf.append("formObject.submit();"+"\n");
strBuf.append("}"+"\n");
//如果选择类型为checkbox,增加全选的脚本
if(this.getSelectType() == OperateConst.SELECT_TYPE_CHECKBOX)
{
//行选择脚本(用于checkBox)
strBuf.append("function doSelectAll()"+"\n");
strBuf.append("{"+"\n");
strBuf.append("var ItemName = '"+this.selectItemName+"'"+"\n");
strBuf.append("var ItemObj ;\n");
strBuf.append("var i ;\n");
strBuf.append("for(i=0;i<"+this.formName+".length;i++)\n");
strBuf.append("{\n");
strBuf.append("if("+this.formName+".elements[i].name == '"+this.selectItemName+"')\n");
strBuf.append("{\n");
strBuf.append("ItemObj = "+this.formName+".elements[i];\n");
strBuf.append("if("+this.formName+".checkAll.checked == true)\n");
strBuf.append("{\n");
strBuf.append("ItemObj.checked = true;\n");
strBuf.append("}\n");
strBuf.append("else\n");
strBuf.append("{\n");
strBuf.append("ItemObj.checked = false;\n");
strBuf.append("}\n");
strBuf.append("}\n");
strBuf.append("}\n");
strBuf.append("}\n");
}
strBuf.append("</script>\n");
if(this.formName.equals(""))
{
strBuf.append("</form>");
}
return strBuf.toString();
}
/**
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_sPageUrl jsp页面路径
* @param pm_sSql 查询的sql
* @param request SevletRequest
* @param response SevletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize, int pm_iPageIndex, String pm_sSql, HttpServletRequest request, HttpServletResponse response) throws OperateException {
//检查必要设置
if(this.con == null)
{
throw new OperateException(OperateConst.EXCEPTION_TYPE_CON_IS_NULL);
}
this.contentTitle = new String[100];
PageDbBean dbBean = new PageDbBean(con);
Vector result = dbBean.getResultBySql(pm_sSql,this.contentTitle);
this.setDataVector(result);
return getContentHtml(pm_iPageSize,pm_iPageIndex,request,response);
}
/**
* @param pm_iPageSize 页大小
* @param pm_iPageIndex 页码
* @param pm_sPageUrl jsp页面路径
* @param pm_sSql 查询的sql
* @param pm_sDBType 数据库类型,如数据库直接支持数据库端分页,可提高效率
* @param request ServletRequest
* @param response ServletResponse
* @return 数据库分页后的Table HTML(带分页按钮)
* @throws OperateException
*/
public String getContentHtml(int pm_iPageSize, int pm_iPageIndex, String pm_sSql, String pm_sDBType, HttpServletRequest request, HttpServletResponse response) throws OperateException {
//判断数据库类型,如支持数据库端分页,重新组装sql
String sql = pm_sSql;
if(pm_sDBType.equals(OperateConst.DBTYPE_ORACLE))/*支持数据库端分页*/
{
//检查必要设置
if(this.con == null)
{
throw new OperateException(OperateConst.EXCEPTION_TYPE_CON_IS_NULL);
}
//检查必要设置
if(this.pageUrl == null) /*如传递的跳转页面为空,抛出异常*/
{
throw new OperateException(OperateConst.EXCEPTION_TYPE_URL_IS_NULL);
}
//主体操作
StringBuffer strBuf = new StringBuffer("");
int pageSize=pm_iPageSize,pageIndex=pm_iPageIndex;
// 判断是第一次进入还是翻页操作,设置相应的pageSize和pageIndex
if(request.getParameter("pageSizeText")==null)
{
pageSize = pm_iPageSize;
}
else if(!request.getParameter("pageSizeText").equals(""))
{
pageSize = Integer.parseInt(request.getParameter("pageSizeText"));
}
if(request.getParameter("pageIndex")==null)
{
pageIndex = pm_iPageIndex;
}
else if(request.getParameter("pageIndex")!=null)
{
pageIndex = Integer.parseInt(request.getParameter("pageIndex"));
}
//如跳转类型为Action,在url中加flag=pageOperate参数以便在用户的Action中标识跳转操作(默认为跳转到原jsp)
if(this.forwardType == OperateConst.FOWARD_TYPE_ACTION)
{
this.pageUrl = this.pageUrl+"?flag=pageOperate&";
}
else if(this.forwardType == OperateConst.FOWARD_TYPE_JSP)
{
this.pageUrl = this.pageUrl+"?";
}
int pageBegin = pageSize*pageIndex;
int pageEnd = pageSize*(pageIndex+1)-1;
String pageUrlReal = this.pageUrl;
int pageTotal = 0;
sql = "select * from (select rownum r, t. * from ("+pm_sSql +") t where rownum <="+(pageEnd+1)+") t_out where r >="+(pageBegin+1);
//获取总页数
PageDbBean dbBean = new PageDbBean(this.con);
int rowTotal = dbBean.getTotalCount(pm_sSql);
pageTotal = rowTotal/pm_iPageSize;
if(pageTotal*pm_iPageSize<rowTotal)
{
pageTotal = pageTotal + 1;
}
//如未设置form名,则将分页数据封装在名为"form1"的表单中
if(this.getFormName().equals(""))
{
this.setFormName("form1");
this.setSelfForm(true);
strBuf.append("<form name="+this.formName+" action='' method=post>");
strBuf.append("\n");
}
else if( (!this.getFormName().equals(""))&&this.isSelfForm==true)
{
this.setFormName("form1");
strBuf.append("<form name="+this.formName+" action='' method=post>");
strBuf.append("\n");
}
//主体操作
this.contentTitle = new String[100];
Vector result = dbBean.getResultBySql2(sql,this.contentTitle);
Vector vecContent = result;
strBuf.append("<table width=100% cellspacing=1 cellpadding=0>");
//表格标题栏
strBuf.append("<tr >");
if(this.hasSelectItem)
{
strBuf.append("<td >");
if(this.selectType == OperateConst.SELECT_TYPE_CHECKBOX)
{
strBuf.append("<input type='checkbox' name='checkAll' onclick='doSelectAll();'/>");
}
strBuf.append("</td>");
}
for(int i=0;i<contentTitle.length;i++)
{
strBuf.append("<td>"+contentTitle[i]+"</td>");
}
strBuf.append("</tr>");
for(int i=0;i<vecContent.size();i++)
{
Vector tmp = (Vector)vecContent.get(i);
strBuf.append("<tr>");
if(this.hasSelectItem)
{
strBuf.append("<td>");
if(this.selectType == OperateConst.SELECT_TYPE_CHECKBOX)
{
strBuf.append("<input type='checkbox' name='"+this.getSelectItemName()+"' value='");
for(int k=0;k<this.selectKeyArray.length;k++)
{
strBuf.append(tmp.get(selectKeyArray[k]-1)+";");
}
strBuf.append("'/>");
}
else if(this.selectType == OperateConst.SELECT_TYPE_RADIOBUTTON)
{
strBuf.append("<input type='radio' name='"+this.getSelectItemName()+"' value='");
for(int k=0;k<this.selectKeyArray.length;k++)
{
strBuf.append(tmp.get(selectKeyArray[k]-1)+";");
}
strBuf.append("'/>");
}
strBuf.append("</td>");
}
for(int j=0;j<tmp.size();j++)
{
strBuf.append("<td>"+tmp.get(j)+"</td>");
}
strBuf.append("</tr>");
}
strBuf.append("</table>");
strBuf.append("<br>");
if(pageIndex>0)
{
strBuf.append("|<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex=0' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>首页</font>");
strBuf.append("</a>|");
strBuf.append("<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex="+String.valueOf(pageIndex-1)+"' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>上一页</font>");
strBuf.append("</a>|");
}
if(pageIndex<pageTotal-1)
{
strBuf.append("<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex="+String.valueOf(pageIndex+1)+"' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>下一页</font>");
strBuf.append("</a>|");
strBuf.append("<a href='"+pageUrlReal+"pageSize="+String.valueOf(pageSize)+"&pageIndex="+String.valueOf(pageTotal-1)+"' onClick=\"commonSubmit("+this.formName+",this.href);return false;\">");
strBuf.append("<font color='red'>末页</font>");
strBuf.append("</a>|");
}
strBuf.append("<font color='red'>每页显示</font>");
strBuf.append("<input type=text name='pageSizeText' size=10 value="+pageSize+">");
strBuf.append("</input>");
strBuf.append("<font color='red'>行</font>");
//js脚本
strBuf.append("\n");
strBuf.append("<script language='JavaScript'>\n");
//提交翻页操作脚本
strBuf.append("function commonSubmit(formObject,url)"+"\n");
strBuf.append("{\n");
strBuf.append("formObject.action=url"+"\n");
//strBuf.append("alert(formObject.action)\n");
strBuf.append("formObject.submit();"+"\n");
strBuf.append("}"+"\n");
//如果选择类型为checkbox,增加全选的脚本
if(this.getSelectType() == OperateConst.SELECT_TYPE_CHECKBOX)
{
//行选择脚本(用于checkBox)
strBuf.append("function doSelectAll()"+"\n");
strBuf.append("{"+"\n");
strBuf.append("var ItemName = '"+this.selectItemName+"'"+"\n");
strBuf.append("var ItemObj ;\n");
strBuf.append("var i ;\n");
strBuf.append("for(i=0;i<"+this.formName+".length;i++)\n");
strBuf.append("{\n");
strBuf.append("if("+this.formName+".elements[i].name == '"+this.selectItemName+"')\n");
strBuf.append("{\n");
strBuf.append("ItemObj = "+this.formName+".elements[i];\n");
strBuf.append("if("+this.formName+".checkAll.checked == true)\n");
strBuf.append("{\n");
strBuf.append("ItemObj.checked = true;\n");
strBuf.append("}\n");
strBuf.append("else\n");
strBuf.append("{\n");
strBuf.append("ItemObj.checked = false;\n");
strBuf.append("}\n");
strBuf.append("}\n");
strBuf.append("}\n");
strBuf.append("}\n");
}
strBuf.append("</script>\n");
if(this.getFormName().equals(""))
{
strBuf.append("</form>");
}
return strBuf.toString();
}
else /*不支持数据库端分页*/
{
return getContentHtml(pm_iPageSize,pm_iPageIndex,pm_sSql,request,response);
}
}
//属性get/set方法
public String getFormName() {
return formName;
}
public void setFormName(String formName) {
this.formName = formName;
}
public String[] getContentTitle() {
return contentTitle;
}
public void setContentTitle(String[] contentTitle) {
this.contentTitle = contentTitle;
}
public String getSelectItemName() {
return selectItemName;
}
public void setSelectItemName(String selectItemName) {
this.selectItemName = selectItemName;
}
public int[] getSelectKeyArray() {
return selectKeyArray;
}
public void setSelectKeyArray(int[] selectKeyArray) {
this.selectKeyArray = selectKeyArray;
}
public int getSelectType() {
return selectType;
}
public void setSelectType(int selectType) {
this.selectType = selectType;
}
public boolean hasSelectItem() {
return hasSelectItem;
}
public void setHasSelectItem(boolean hasSelectItem) {
this.hasSelectItem = hasSelectItem;
}
public int getForwardType() {
return forwardType;
}
public void setForwardType(int forwardType) {
this.forwardType = forwardType;
}
public String getPageUrl() {
return pageUrl;
}
public void setPageUrl(String pageUrl) {
this.pageUrl = pageUrl;
}
public void setSelfForm(boolean isSelfForm) {
this.isSelfForm = isSelfForm;
}
public void setDataVector(Vector dataVector) {
this.dataVector = dataVector;
}
public void setCon(Connection con) {
this.con = con;
}
}
PageDbBean.java 文件
package Action;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Vector;
public class PageDbBean {
private Connection con;
private Statement stmt;
private ResultSet rs;
public PageDbBean(Connection pm_objCon)
{
con = pm_objCon;
}
/**
* @param pm_sSql sql语句
* @param pm_sTitle 设置标题
* @return 结果集
*/
public Vector getResultBySql(String pm_sSql,String [] pm_sTitle)
{
try
{
stmt = this.con.createStatement();
rs = stmt.executeQuery(pm_sSql);
Vector result = new Vector();
ResultSetMetaData meta = rs.getMetaData();
int colNum = meta.getColumnCount();
for(int j=0;j<colNum;j++)
{
pm_sTitle[j] = meta.getColumnName(j+1);
}
while(rs.next())
{
Vector rowVector = new Vector();
for(int i=0;i<colNum;i++)
{
rowVector.add(rs.getString(i+1));
}
result.add(rowVector);
}
return result;
}
catch(SQLException es)
{
System.out.println("PageDbBean:SQL操作异常!");
es.printStackTrace();
return null;
}
finally
{
try {
rs.close();
stmt.close();
}
catch (SQLException e)
{
System.out.println("资源回收失败!");
e.printStackTrace();
}
}
}
/**
* @param pm_sSql sql语句
* @param pm_sTitle 设置标题
* @return 结果集
*/
public Vector getResultBySql2(String pm_sSql,String [] pm_sTitle)
{
try
{
stmt = this.con.createStatement();
rs = stmt.executeQuery(pm_sSql);
Vector result = new Vector();
ResultSetMetaData meta = rs.getMetaData();
int colNum = meta.getColumnCount();
for(int j=1;j<colNum;j++)
{
pm_sTitle[j-1] = meta.getColumnName(j+1);
}
while(rs.next())
{
Vector rowVector = new Vector();
for(int i=1;i<colNum;i++)
{
rowVector.add(rs.getString(i+1));
}
result.add(rowVector);
}
return result;
}
catch(SQLException es)
{
System.out.println("PageDbBean:SQL操作异常!");
es.printStackTrace();
return null;
}
finally
{
try {
rs.close();
stmt.close();
}
catch (SQLException e)
{
System.out.println("资源回收失败!");
e.printStackTrace();
}
}
}
/**
* @param pm_sSql
* @return 总记录数
*/
public int getTotalCount(String pm_sSql)
{
try
{
stmt = con.createStatement();
rs = stmt.executeQuery("select count(*) from ("+pm_sSql+")");
rs.next();
int rowTotal = rs.getInt(1);
return rowTotal;
}
catch(SQLException es)
{
System.out.println("PageDbBean:SQL操作异常");
es.printStackTrace();
return -1;
}
finally
{
try
{
this.rs.close();
this.stmt.close();
}
catch(SQLException es2)
{
System.out.println("PageDbBean:资源回收失败!");
es2.printStackTrace();
}
}
}
}
posted on 2007-08-06 16:21
蛮哥♂枫 阅读(1286)
评论(0) 编辑 收藏 所属分类:
Java