jsp分页
来源:互联网 发布:老魏弓淘宝店铺 编辑:程序博客网 时间:2024/06/06 02:39
- Java代码:
- package com.zhaipuhong.common.util;
- import java.util.ArrayList;
- import org.apache.log4j.Logger;
- import javax.servlet.http.HttpServletRequest;
- public class PageBean {
- private static Logger logger = Logger.getLogger(PageBean.class);
- /**
- * 对查询数据封装
- */
- private ArrayList objList = new ArrayList();
- /**
- * 页面记录大小
- */
- private int pageSize = 10;
- /**
- * 总行数
- */
- private int rowcnt;
- /**
- * 当前页码
- */
- private int currentPage;
- /**
- * 分页信息
- */
- private String pageInfo;
- /**
- * 获得当前页
- * @return
- */
- public int getCurrentPage() {
- return currentPage;
- }
- /**
- * 设置当前页
- * @param currentPage
- */
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- /**
- * 得到页面大小
- * @return
- */
- public int getPageSize() {
- return pageSize;
- }
- /**
- * 设置页面大小
- * @param pageSize
- */
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- /**
- * 得到数据
- * @return
- */
- public ArrayList getObjList() {
- return objList;
- }
- /**
- * 设置数据
- * @param objList
- */
- public void setObjList(ArrayList objList) {
- this.objList = objList;
- }
- /**
- * 设置页面信息
- * @param pageInfo
- */
- public void setPageInfo(String pageInfo) {
- this.pageInfo = pageInfo;
- }
- /**
- * 实现下拉框数字翻页
- * @param pageNum 当前页号
- * @param lastPageNum 总的页号
- * @return
- */
- private String getSelect(int pageNum, int lastPageNum) {
- String select = "跳到<select name=temp onchange=/"prePage(this.value)/">";
- select = select + "<option value=" + pageNum + ">" + pageNum + "</option>";
- for (int i = 1; i <= lastPageNum; i++) {
- if (i != pageNum) {
- select = select + "<option value=" + i + ">" + i + "</option>";
- }
- }
- select = select + "</select>页";
- return select;
- }
- /**
- * 获取翻页信息
- * @param req
- * @return
- */
- public String getPageInfo() {
- int previousPageNum = currentPage - 1;
- int nextPageNum = currentPage + 1;
- int pageCount = rowcnt % this.pageSize;
- if (pageCount == 0) {
- pageCount = rowcnt / pageSize;
- }
- else {
- pageCount = rowcnt / pageSize + 1;
- }
- if (pageCount < 1) {
- pageCount = 1;
- }
- pageInfo = "当前第<font color=/"red/">" + currentPage +
- "</font>页  共<font color=/"red/">" + pageCount +
- "</font>页  ";
- String first = "<a href =/"#/" onclick=/"prePage('" + 1 +
- "')/">首页 </a>";
- String previous = "<a href =/"#/" onclick=/"prePage('" + previousPageNum +
- "')/">前一页 </a>";
- String next = "<a href =/"#/" onclick=/"prePage('" + nextPageNum +
- "')/">下一页 </a>";
- String last = "<a href =/"#/" onclick=/"prePage('" + pageCount +
- "')/">末页</a>";
- if (currentPage != 1) {
- pageInfo = pageInfo.concat(first).concat(previous);
- }
- if (currentPage != pageCount) {
- pageInfo = pageInfo.concat(next).concat(last);
- }
- pageInfo = pageInfo + getSelect(currentPage, pageCount);
- pageInfo += " <input name=/"pagenum/" type=/"hidden/">/r/n";
- String script = "<script>/r/n";
- script += "function prePage(pageNum){/r/n"; // document.all["pageNum"].value=pageNum;
- script += "document.all[/"pagenum/"].value=pageNum;/r/n";
- script += "document.forms[0].submit();/r/n";
- script += "}/r/n";
- script += "</script>";
- pageInfo = pageInfo + script;
- return pageInfo == null ? "" : pageInfo;
- }
- /**
- * 设置当前页码
- * @param httpServletRequest
- */
- public void setPageNum(HttpServletRequest httpServletRequest) {
- String pageNum = httpServletRequest.getParameter("pagenum");
- try {
- this.setCurrentPage(Integer.parseInt(pageNum));
- }
- catch (Exception ex) {
- this.setCurrentPage(1);
- }
- }
- /**
- * 得到总行数
- * @return
- */
- public int getRowcnt() {
- return rowcnt;
- }
- /**
- * 设置总行数
- * @param rowcnt
- */
- public void setRowcnt(int rowcnt) {
- this.rowcnt = rowcnt;
- }
- }
java query:
- ……
- ArrayList list = new ArrayList();
- AA a= null;
- ResultSet rs = null;
- try{
- ……
- String sqlCnt = "select count(1) as cnt from (" + sqlStatement.toString() + ")";
- String sql = "select * "
- + "from (select temp.*, rownum serialno "
- + "from (" + sqlStatement.toString() + ") temp) page "
- + "where page.serialno between " + (pageBean.getPageSize()*(pageBean.getCurrentPage() - 1) + 1)
- + " and " + pageBean.getPageSize()*pageBean.getCurrentPage();
- rs = this.stmt.executeQuery(sqlCnt);
- if(rs.next()){
- pageBean.setRowcnt(rs.getInt("cnt"));
- }
- rs.close();
- rs = this.stmt.executeQuery(sql);
- while(rs.next()){
- a = new AA();
- a.setID(rs.getInt("ID"));
- a.setName(rs.getString("name"));
- list.add(a);
- }
- ……
- pageBean.setObjList(policyList);
页面代码:
- <html:form action="/xxxxAction">
- <table width="98%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#7FA9E3">
- <tr>
- <td bgcolor="#FFFFFF" class="box_flow_t">查询结果</td>
- </tr>
- <tr>
- <td height="186" valign="top" id="box_flow_m21">
- <table width="100%" border="1" align="center" cellpadding="2" cellspacing="0">
- <tr>
- <td height="26"><div align="center"><strong>编号</strong></div>
- </td>
- <td><div align="center"><strong>名称</strong></div>
- </td>
- </tr>
- <logic:present name="pageBean">
- <logic:iterate id="a" name="pageBean" property="objList" scope="request">
- <tr>
- <td width="12%"> <bean:write name="a" property="ID"/>
- </td>
- <td width="14%"> <bean:write name="a" property="Name"/>
- </td>
- </tr>
- </logic:iterate>
- </table>
- <table width="100%" height="34" border="0" align="center" cellpadding="2" cellspacing="0">
- <tr>
- <td height="34"><div align="left"><bean:write name="pageBean" property="pageInfo" filter="false"/></div></td>
- </tr>
- </table>
- </logic:present></td>
- </tr>
- <html:hidden property="action"/> //这里是个逻辑跳转变量
- </table>
- ……
- </html:form>
- action代码:
- ……
- PageBean pageBean = new PageBean();
- pageBean.setPageNum(request);
- business.getPolicyList(xxx, pageBean);//xx是假定的参数
- request.setAttribute("pageBean", pageBean);
- form.setAction("xxxx"); //注意因为是分页,下一次还是这action逻辑
- return actionMapping.findForward("xxxx"); //跳转到分页显示页面
- ……
- jsp分页
- JSP分页
- Jsp分页
- jsp分页
- jsp分页
- JSP分页
- jsp 分页
- jsp分页
- jsp分页
- JSP分页
- JSP分页
- jsp分页
- jsp分页
- JSP 分页
- jsp分页
- Jsp分页
- jsp分页
- JSP分页
- Silverlight常见问题及解决方法
- DWR传值
- C# 集合类
- ASP.NET讲座(2)-ASP.NET控件
- 软件项目需求调研总结
- jsp分页
- HTTP请求错误400、401、402、403、404、405、406、407、412、414、500、501、502解析
- asp.net断点续传技术
- ASP.NET讲座(1)-ASP.NET简介
- 用VB把asp封装成dll
- 转贴:VC6.0下配置boost库使用正则表达式
- asp将数据库的数据转换成excel导出
- 二十一天学通C++之异常处理机制
- struts2+ajax的应用