⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tablemodelbean.java

📁 ajax+struts最简单代码
💻 JAVA
字号:
/*
 * Created on 2005-8-1
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.mstar.strutsajax.form;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.mstar.strutsajax.ModelOneDAO;

/**
 * @author matianyi
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public class TableModelBean {
	
	//表格的第一列
	public static final int COLUMN_1 = 0;
	
	//表格的第二列
	public static final int COLUMN_2 = 1;
	
	//表格的第三列
	public static final int COLUMN_3 = 2;

	//每一列的排序升序降序标记 true升序,false降序
	private boolean[] columnFlags = { false, false, false };
	
	//表格分页总页面数
	private int totalPage = 0;
	
	//表格当前页
	private int currentPage = 0;
	
	//表格总行数
	private int rowsCount = 0;

	//没用
	private String[] pagers = { "" };

	//存放全体记录的容器
	private List rows = new ArrayList();

	//存放当前记录的容器
	private List currentPageRows = new ArrayList();

	//数据库操作类
	private static ModelOneDAO dao;

	//每页记录数设为20
	private static final int PAGE_SIZE = 20;

	//初始排序行为第一行
	private int sortedColumn = 1;

	/**
	 *  构造函数
	 */
	public TableModelBean() {
		dao = new ModelOneDAO();
		init();
	}

	/**
	 *  初始化
	 */
	private void init() {
		try {
			rows = dao.getSortedRows(sortedColumn, columnFlags[sortedColumn]);
			setRowsCount(rows.size());
			setTotalPage(getTotalPageByRow(rows.size(), PAGE_SIZE));
			setCurrentPage(1);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 返回当前页的内容
	 * @return Returns the currentPage.
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * 设置当前页
	 * @param currentPage
	 *            The currentPage to set.
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;

		currentPageRows.clear();
		int firstIndex = PAGE_SIZE * (currentPage - 1);
		int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
				+ PAGE_SIZE : rowsCount;
		for (int i = firstIndex; i < lastIndex; i++) {
			currentPageRows.add(rows.get(i));
		}
	}

	/**
	 * 取得所有行
	 * @return Returns the rows.
	 */
	public List getRows() {
		return rows;
	}


	/**
	 * 取的分页数
	 * @return Returns the totalPage.
	 */
	public int getTotalPage() {
		init();
		return totalPage;
	}

	/**
	 * 设置分页数
	 * @param totalPage
	 *            The totalPage to set.
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**
	 * 取得纪录数
	 * @return Returns the totalRows.
	 */
	public int getRowsCount() {
		return rowsCount;
	}

	/**
	 *	设置记录数
	 *  @param totalRows
	 *            The totalRows to set.
	 */
	public void setRowsCount(int rowsCount) {
		this.rowsCount = rowsCount;
	}

	/**
	 * 取得当前页中的记录数
	 * @return Returns the currentPageRows.
	 */
	public List getCurrentPageRows() {
		return currentPageRows;
	}

	/**
	 * 取得page页中的记录,当page大于totalPage时返回最后页
	 * 因为是上面的getCurrentPageRows函数的重载,所以在dwr中不能正常使用。
	 * 于是出现了getRowsByPageNo方法。
	 * @param page
	 * @return the currentPageRows.
	 */
	public List getCurrentPageRows(int page) {
		currentPageRows.clear();
		int firstIndex = PAGE_SIZE * (page - 1);
		int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
				+ PAGE_SIZE : rowsCount;
		for (int i = firstIndex; i < lastIndex; i++) {
			currentPageRows.add(rows.get(i));
		}
		return currentPageRows;
	}

	/**
	 * 取得page页中的记录,当page大于totalPage时返回最后页
	 * @param page
	 * @return 包含当前页记录的List
	 */
	public List getRowsByPageNo(int page) {
		init();
		page = page > totalPage ? totalPage : page;
		List result = new ArrayList();
		int firstIndex = PAGE_SIZE * (page - 1);
		int lastIndex = (firstIndex + PAGE_SIZE) < rowsCount ? firstIndex
				+ PAGE_SIZE : rowsCount;
		for (int i = firstIndex; i < lastIndex; i++) {
			result.add(rows.get(i));
		}
		return result;
	}

	/**
	 * 按照某一列进行排序,再返回当前页中的数据
	 * @param currentPage
	 * @param columnNo
	 * @return the Rows of current Page that sorted by columnNo
	 */
	public List getCurrentPageSortedByColumnRows(int currentPage, int columnNo) {
		init();
		sortBy(columnNo);
		currentPageRows.clear();
		int firstIndex = 20 * (currentPage - 1);
		int lastIndex = (firstIndex + 20) < rowsCount ? firstIndex + 20
				: rowsCount;
		for (int i = firstIndex; i < lastIndex; i++) {
			currentPageRows.add(rows.get(i));
		}
		return currentPageRows;
	}

	/**
	 * 返回一个分页数组。用处不太大,客户端用Javascript也可以计算。
	 * @return Returns the pages.
	 */
	public String[] getPagers() {
		pagers = new String[totalPage];
		for (int i = 1; i <= totalPage; i++) {
			pagers[i - 1] = i + "";
		}
		return pagers;
	}

	/**
	 * 按照某一列进行排序
	 * @param columnNo
	 */
	public void sortBy(int columnNo) {
		this.sortedColumn = columnNo;
		columnFlags[columnNo] = (!columnFlags[columnNo]);
		try {
			rows = dao.getSortedRows(columnNo, columnFlags[columnNo]);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * 删除某一列,按照主键(第一列)
	 * @param key
	 * @return
	 */
	public boolean deleteRow(int key) {
		try {
			dao.deleteRow(key);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 要新增加一个数据前先计算出Id.
	 * 这个例子只是用来演示用的,如果多人访问会出现并发问题
	 * @return
	 */
	public int getNextId() {
		try {
			return dao.getNextId();
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	/**
	 * 增加一行
	 * @param trb
	 * @return
	 */
	public boolean addRow(TableRowBean trb) {
		try {
			dao.addRow(trb);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 更改一行
	 * @param trb
	 * @return
	 */
	public boolean updateRow(TableRowBean trb) {
		try {
			dao.updateRow(trb);
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
	}

	/**
	 * 按照key取回单行信息
	 * @param key
	 * @return
	 */
	public TableRowBean getSingleRow(int key) {
		TableRowBean row;
		try {
			row = dao.getSingleRow(key);
		} catch (SQLException e) {
			row = new TableRowBean();
			e.printStackTrace();
		}
		return row;
	}

	/**
	 * 辅助方法计算分页数
	 * @param rowSize
	 * @param pageSize
	 * @return
	 */
	private static int getTotalPageByRow(int rowSize, int pageSize) {
		int result = 0;
		result = rowSize % pageSize == 0 ? rowSize / pageSize : rowSize
				/ pageSize + 1;
		return result;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -