📄 abstractpage.java
字号:
package util.database.datalist;import java.sql.*;/** * AbstractPage是一个使用{@link ResultSetPage ResultSetPage}的抽象类。 * 这个抽象类中的最重要的一个变量是Connection类型的conn,子类必须实现 * 初始化这个变量的方法。<br> * 抽象类中封装了用于分页显示查询结果的大部分操作,包括: * <pre> * 使用{@link getColumns() getColumns()}取得数组形式的被查询的字段名称 * 使用{@link getRecords() getRecords()}以二维数组的形式取得页面上显示的数据 * 使用{@link getNavigator(String) getNavigator(String)}得到导航条的html代码 * </pre> * AbstractPage的子类必须实现下面的抽象方法,包括 * <pre> * 初始化conn的方法{@link initiateConnection() initiateConnection()} * 设置查询结果的方法{@link doQuery() doQuery()} * </pre> * 基本使用方法举例: * <pre> * public class ShowPageBean extends AbstractPage * { * public ShowPageBean() throws Exception{} * * public void initiateConnection() throws Exception * { * //使用用户的设定改变默认值countPerPage=5 * this.countPerPage = Config.getCountPerPage(); * this.conn = Pool.getConnection(); * } * * public ResultSet doQuery() throws Exception * { * //do some SQL query here * ResultSet rs = ... * return rs; * } * } * </pre> * * @author Michael Zeng * @version 1.0 September 24, 2002 */public abstract class AbstractPage{ protected Connection conn = null; protected ResultSetPage page = null; protected DataList chunk = null; protected DataListIterator it = null; protected int startIndex = 0, countPerPage = 5; protected String navigatorSymbol[] = new String[]{ "<--", "-->" }; protected String navigatorColor[] = new String[]{ "color:blue","color:red" }; public void setStartIndex(int startIndex){ this.startIndex = startIndex; } public int getStartIndex(){ return this.startIndex; } /** * protected构造方法 * * @throws Exception */ protected AbstractPage() throws Exception { initiateConnection(); page = new ResultSetPage(conn, doQuery()); } /** * 抽象方法,用于初始化基类中的Connection对象。使用Template Pattern,具体 * 实现延迟到子类中定义。 * * @throws Exception */ abstract public void initiateConnection()throws Exception; /** * 抽象方法,用于设置查询结果。使用Template Pattern,具体实现延迟到 * 子类中定义。 * * @return ResultSet 用于显示的查询结果 * @throws Exception */ abstract public ResultSet doQuery() throws Exception; /** * 得到被查询的字段名称数组 * * @return String[] 字段名称数组 */ public String[] getColumns() { if(page == null) return new String[0]; else return page.getColumns(); } /** * 得到指定范围内的结果值。二维数组表示所有的结果值,第一维表示一行独立的记 * 录,第二维表示一条记录中的每个值。 * * @return String[][] 指定范围内的结果值 * @throws Exception */ public String[][] getRecords() throws Exception { if(page == null) return new String[0][0]; //取得指定范围的子集,根据子集大小设置返回值的大小 chunk = page.getListChunk(startIndex, countPerPage); String[][] records = new String[chunk.size()][]; it = chunk.iterator(); int i = 0; while (it.hasNext()) { records[i] = it.next(); i++; } return records; } /** * 得到一段分页显示使用的导航条的html代码。默认样式如下: * <p style="color:blue"><-- 1 2 3 <font color="red">4</font> 5 --></p> * * @param targetPage 需要刷新的页面名称,一般情况是当前页的名称。 * @return 导航条的html代码 * @throws SQLException */ public String getNavigator(String targetPage) throws SQLException { StringBuffer sb = new StringBuffer(""); //使用Composite Pattern,私有方法对目标StringBuffer进行改写 appendSymbolNavigator(sb, targetPage, true); appendNumNavigator(sb, targetPage); appendSymbolNavigator(sb, targetPage, false); return sb.toString(); } /** * 释放资源(不包括数据库连接) * * @throws SQLException */ public void close() throws SQLException { if(page != null) { page.close(); page = null; } if(chunk != null) { chunk = null; } } /** * package方法,在给定的StringBuffer对象末尾追加导航符的html代码。 * * @param sb 给定的StringBuffer对象 * @param targetPage 需要刷新的页面名称 * @param isPrevious true表示PREVIOUS符号,false表示NEXT符号 * @throws SQLException */ protected void appendSymbolNavigator(StringBuffer sb, String targetPage, boolean isPrevious) throws SQLException { if(isPrevious && !page.hasPrevious()) return; if(!isPrevious && !page.hasNext()) return; int targetIndex = startIndex + countPerPage; String targetSymbol = navigatorSymbol[1]; if(isPrevious) { targetIndex = startIndex - countPerPage; targetSymbol = navigatorSymbol[0]; } sb.append("<a style='text-decoration: none' href='"); sb.append(targetPage).append("?startIndex=").append(targetIndex); sb.append("'>").append(targetSymbol).append("</a> "); } /** * package方法,在给定的StringBuffer对象末尾追加导航页数字的html代码。 * * @param sb 给定的StringBuffer对象 * @param targetPage 需要刷新的页面名称 * @throws SQLException */ protected void appendNumNavigator(StringBuffer sb, String targetPage) throws SQLException { int total = page.size()/countPerPage + 1; String color = navigatorColor[0]; for(int i = 0; i < total; i++) { if( startIndex/countPerPage == i) color = navigatorColor[1]; else color = navigatorColor[0]; sb.append("<a style='text-decoration: none;").append(color + "' "); sb.append("href='").append(targetPage).append("?startIndex="); sb.append(i * countPerPage).append("'>").append(i + 1).append("</a> "); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -